Skip to content

Commit

Permalink
Merge pull request #118 from Laravel-Backpack/pr/111
Browse files Browse the repository at this point in the history
Add ability to change settings table name and route
  • Loading branch information
tabacitu authored Sep 22, 2021
2 parents a60bbe3 + d6b7e5e commit 55857e4
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 16 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ An interface for the administrator to easily change application settings. Uses L
> ### Security updates and breaking changes
> Please **[subscribe to the Backpack Newsletter](http://backpackforlaravel.com/newsletter)** so you can find out about any security updates, breaking changes or major features. We send an email every 1-2 months.
## Install
## Install

**Note:** The default table name is `settings`, if you need to change it please carefully read the comments in the instruction below.

In your terminal:

``` bash
# install the package
composer require backpack/settings

# run the migration
# [optional] if you need to change table name or migration name, please do it now before proceding
php artisan vendor:publish --provider="Backpack\Settings\SettingsServiceProvider" --tag="config"
# then change the values you need in in `config/backpack/settings.php`

# publish & run the migration
php artisan vendor:publish --provider="Backpack\Settings\SettingsServiceProvider"
php artisan migrate

Expand Down
24 changes: 17 additions & 7 deletions src/SettingsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,39 @@ class SettingsServiceProvider extends ServiceProvider
*/
public function boot()
{
// use the vendor configuration file as fallback
$this->mergeConfigFrom(
__DIR__.'/config/backpack/settings.php',
'backpack.settings'
);

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

// only use the Settings package if the Settings table is present in the database
if (!\App::runningInConsole() && count(Schema::getColumnListing('settings'))) {
if (!\App::runningInConsole() && count(Schema::getColumnListing(config('backpack.settings.table_name')))) {
// get all settings from the database
$settings = Setting::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) {
Config::set('settings.'.$setting->key, $setting->value);
$prefixed_key = !empty($config_prefix) ? $config_prefix.'.'.$setting->key : $setting->key;
Config::set($prefixed_key, $setting->value);
}
}
// publish the migrations and seeds
$this->publishes([__DIR__.'/database/migrations/' => database_path('migrations')], 'migrations');
$this->publishes([
__DIR__.'/database/migrations/create_settings_table.php.stub' => database_path('migrations/'.config('backpack.settings.migration_name').'.php'),
], 'migrations');

// publish translation files
$this->publishes([__DIR__.'/resources/lang' => resource_path('lang/vendor/backpack')], 'lang');

// publish setting files
$this->publishes([__DIR__.'/config' => config_path()], 'config');
}

/**
Expand Down Expand Up @@ -80,10 +94,6 @@ public function setupRoutes(Router $router)
*/
public function register()
{
$this->app->bind('settings', function ($app) {
return new Settings($app);
});

// register their aliases
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Setting', \Backpack\Settings\app\Models\Setting::class);
Expand Down
2 changes: 1 addition & 1 deletion src/app/Http/Controllers/SettingCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function setup()
{
CRUD::setModel("Backpack\Settings\app\Models\Setting");
CRUD::setEntityNameStrings(trans('backpack::settings.setting_singular'), trans('backpack::settings.setting_plural'));
CRUD::setRoute(backpack_url('setting'));
CRUD::setRoute(backpack_url(config('backpack.settings.route')));
}

public function setupListOperation()
Expand Down
12 changes: 10 additions & 2 deletions src/app/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ class Setting extends Model
{
use CrudTrait;

protected $table = 'settings';
protected $fillable = ['value'];

public function __construct(array $attributes = [])
{
parent::__construct($attributes);

$this->table = config('backpack.settings.table_name');
}

/**
* Grab a setting value from the database.
*
Expand Down Expand Up @@ -40,7 +46,9 @@ public static function get($key)
*/
public static function set($key, $value = null)
{
$prefixed_key = 'settings.'.$key;
$database_prefix = config('backpack.settings.database_prefix');

$prefixed_key = !empty($database_prefix) ? $database_prefix.'.'.$key : $key;
$setting = new self();
$entry = $setting->where('key', $key)->firstOrFail();

Expand Down
50 changes: 50 additions & 0 deletions src/config/backpack/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Table Name
|--------------------------------------------------------------------------
|
| Database Settings Table Name
|
*/
'table_name' => 'settings',

/*
|--------------------------------------------------------------------------
| Route
|--------------------------------------------------------------------------
|
| URL Segment aka route to the Settings panel.
|
*/
'route' => 'setting',

/*
|--------------------------------------------------------------------------
| Config Prefix
|--------------------------------------------------------------------------
|
| 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')
|
| 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.
|
*/
'config_prefix' => 'settings',

/*
|--------------------------------------------------------------------------
| Migration file name
|--------------------------------------------------------------------------
|
| The file name for the settings migration file. It will be created in database_path('/migrations/%name_you_choose%.php)
| Note: .php extension is automatically added.
|
*/
'migration_name' => '2015_08_04_131614_create_settings_table',
];
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CreateSettingsTable extends Migration
*/
public function up()
{
Schema::create('settings', function (Blueprint $table) {
Schema::create(config('backpack.settings.table_name'), function (Blueprint $table) {
$table->increments('id');
$table->string('key')->unique();
$table->string('name');
Expand All @@ -31,6 +31,6 @@ public function up()
*/
public function down()
{
Schema::drop('settings');
Schema::drop(config('backpack.settings.table_name'));
}
}
2 changes: 1 addition & 1 deletion src/database/seeds/SettingsTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class SettingsTableSeeder extends Seeder
public function run()
{
foreach ($this->settings as $index => $setting) {
$result = DB::table('settings')->insert($setting);
$result = DB::table(config('backpack.settings.table_name'))->insert($setting);

if (!$result) {
$this->command->info("Insert failed at record $index.");
Expand Down
2 changes: 1 addition & 1 deletion src/routes/backpack/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
'prefix' => config('backpack.base.route_prefix', 'admin'),
'middleware' => ['web', backpack_middleware()],
], function () {
Route::crud('setting', 'SettingCrudController');
Route::crud(config('backpack.settings.route'), 'SettingCrudController');
});

0 comments on commit 55857e4

Please sign in to comment.