Skip to content

Commit

Permalink
Merge pull request #135 from Laravel-Backpack/pr/134
Browse files Browse the repository at this point in the history
Pr/134
  • Loading branch information
pxpm authored Apr 2, 2024
2 parents eebf5a3 + 572730d commit 1d80f09
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
25 changes: 12 additions & 13 deletions src/SettingsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

namespace Backpack\Settings;

use Backpack\Settings\app\Models\Setting;
use Config;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;

class SettingsServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
* Indicates if the loading of the provider is deferred.
*
* @var bool
*/
Expand All @@ -38,20 +36,23 @@ public function boot()
);

// define the routes for the application
$this->setupRoutes($this->app->router);
$this->setupRoutes();

// only use the Settings package if the Settings table is present in the database
if (!\App::runningInConsole() && Schema::hasTable(config('backpack.settings.table_name'))) {
if (!App::runningInConsole() && Schema::hasTable(config('backpack.settings.table_name'))) {
/** @var \Illuminate\Database\Eloquent\Model $modelClass */
$modelClass = config('backpack.settings.model', \Backpack\Settings\app\Models\Setting::class);

// get all settings from the database
$settings = Setting::all();
$settings = $modelClass::all();

$config_prefix = config('backpack.settings.config_prefix');

// bind all settings to the Laravel config, so you can call them like
// Config::get('settings.contact_email')
foreach ($settings as $key => $setting) {
$prefixed_key = !empty($config_prefix) ? $config_prefix.'.'.$setting->key : $setting->key;
Config::set($prefixed_key, $setting->value);
config([$prefixed_key => $setting->value]);
}
}
// publish the migrations and seeds
Expand All @@ -69,13 +70,11 @@ public function boot()
/**
* Define the routes for the application.
*
* @param \Illuminate\Routing\Router $router
*
* @return void
*/
public function setupRoutes(Router $router)
public function setupRoutes()
{
// by default, use the routes file provided in vendor
// by default, use the routes file provided in the vendor
$routeFilePathInUse = __DIR__.$this->routeFilePath;

// but if there's a file with the same name in routes/backpack, use that one
Expand All @@ -95,6 +94,6 @@ public function register()
{
// register their aliases
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Setting', \Backpack\Settings\app\Models\Setting::class);
$loader->alias('Setting', config('backpack.settings.model', \Backpack\Settings\app\Models\Setting::class));
}
}
2 changes: 1 addition & 1 deletion src/app/Http/Controllers/SettingCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SettingCrudController extends CrudController

public function setup()
{
CRUD::setModel("Backpack\Settings\app\Models\Setting");
CRUD::setModel(config('backpack.settings.model', \Backpack\Settings\app\Models\Setting::class));
CRUD::setEntityNameStrings(trans('backpack::settings.setting_singular'), trans('backpack::settings.setting_plural'));
CRUD::setRoute(backpack_url(config('backpack.settings.route')));
}
Expand Down
14 changes: 12 additions & 2 deletions src/config/backpack/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
*/
'table_name' => 'settings',

/*
|--------------------------------------------------------------------------
| Model Name
|--------------------------------------------------------------------------
|
| Settings Eloquent Model Class
|
*/
'model' => \Backpack\Settings\app\Models\Setting::class,

/*
|--------------------------------------------------------------------------
| Route
Expand All @@ -28,11 +38,11 @@
|--------------------------------------------------------------------------
|
| The prefix used to add your settings into the configuration array.
| With this default you can grab your settings with: config('settings.your_setting_key')
| With this default you can grab your settings with config('settings.your_setting_key')
|
| WARNING: WE ADVISE TO NOT LEAVE THIS EMPTY / CHECK IF IT DOES NOT CONFLICT WITH OTHER CONFIG FILE NAMES
|
| - if you leave this empty and your keys match other configuration files you might ovewrite them.
| - if you leave this empty and your keys match other configuration files you might overwrite them.
|
*/
'config_prefix' => 'settings',
Expand Down

0 comments on commit 1d80f09

Please sign in to comment.