Gói này cho phép bạn lưu trữ cài đặt trong kho lưu trữ [cơ sở dữ liệu, Redis,. ] và sử dụng chúng thông qua một ứng dụng mà không gặp rắc rối. Bạn tạo một lớp cài đặt như vậy
class GeneralSettings extends Settings { public string $site_name; public bool $site_active; public static function group[]: string { return 'general'; } }
Khi bạn muốn sử dụng các cài đặt này ở đâu đó trong ứng dụng của mình, bạn có thể đưa chúng vào vì chúng tôi đã đăng ký chúng trong Laravel Container. Ví dụ, trong bộ điều khiển
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }
Bạn có thể cập nhật cài đặt như vậy
class GeneralSettingsController { public function update[ GeneralSettingsRequest $request, GeneralSettings $settings ]{ $settings->site_name = $request->input['site_name']; $settings->site_active = $request->input['site_active']; $settings->save[]; return redirect[]->back[]; } }
Hãy xem cách tạo các lớp cài đặt của riêng bạn
hỗ trợ chúng tôi
Chúng tôi đầu tư rất nhiều tài nguyên vào việc tạo ra các gói nguồn mở tốt nhất trong lớp. Bạn có thể hỗ trợ chúng tôi bằng cách mua một trong những sản phẩm trả phí của chúng tôi
Chúng tôi đánh giá cao việc bạn gửi cho chúng tôi một tấm bưu thiếp từ quê hương của bạn, đề cập đến [những] gói bạn đang sử dụng của chúng tôi. Bạn sẽ tìm thấy địa chỉ của chúng tôi trên trang liên hệ của chúng tôi. Chúng tôi xuất bản tất cả các bưu thiếp nhận được trên bức tường bưu thiếp ảo của chúng tôi
Cài đặt
Bạn có thể cài đặt gói qua nhà soạn nhạc
composer require spatie/laravel-settings
Bạn có thể xuất bản và chạy di chuyển với
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations" php artisan migrate
Bạn có thể xuất bản tệp cấu hình với
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="settings"
Đây là nội dung của tệp cấu hình đã xuất bản
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];
Cách sử dụng
Gói được xây dựng xung quanh các lớp cài đặt, là các lớp có thuộc tính công khai mở rộng từ
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];1. Họ cũng có một phương thức tĩnh
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];2 sẽ trả về một chuỗi
Bạn có thể tạo nhiều nhóm cài đặt, mỗi nhóm có lớp cài đặt của chúng. Ví dụ, bạn có thể có
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];3 với nhóm
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];4 và
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];5 với nhóm
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];6. Tùy thuộc vào bạn làm thế nào để cấu trúc các nhóm này
Mặc dù có thể sử dụng cùng một nhóm cho các lớp cài đặt khác nhau, chúng tôi khuyên bạn không nên sử dụng cùng một nhóm cho nhiều lớp cài đặt
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }3
Bây giờ, bạn sẽ phải thêm lớp cài đặt này vào tệp cấu hình
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];7 trong mảng
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];8 để Laravel có thể tải nó
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }6
Mỗi thuộc tính trong một lớp cài đặt cần một giá trị mặc định phải được đặt trong quá trình di chuyển của nó. Bạn có thể tạo một di chuyển như vậy
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }7
Lệnh này sẽ tạo một tệp mới trong
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];9 nơi bạn có thể thêm các thuộc tính và giá trị mặc định của chúng
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }0
Chúng tôi thêm các thuộc tính
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }30 và
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }31 ở đây vào nhóm
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];4 với các giá trị
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }33 và
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }34. Thông tin thêm về di chuyển sau
Bạn nên di chuyển cơ sở dữ liệu của mình để thêm thuộc tính
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }1
Bây giờ, khi bạn muốn sử dụng thuộc tính
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }30 của lớp cài đặt
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];3, bạn có thể đưa nó vào ứng dụng của mình
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }2
Hoặc sử dụng nó tải nó ở đâu đó trong ứng dụng của bạn như vậy
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }3
Cập nhật cài đặt có thể được thực hiện như vậy
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }4
Lựa chọn một kho lưu trữ
Cài đặt sẽ được lưu trữ và tải từ kho lưu trữ. Có hai loại kho lưu trữ
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }37 và
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }38. Và có thể tạo nhiều kho lưu trữ cho các loại này. Ví dụ: bạn có thể có hai kho lưu trữ
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }37, một kho lưu trữ tới bảng
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];8 trong cơ sở dữ liệu của bạn và một kho lưu trữ khác tới bảng
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }61
Bạn có thể thiết lập rõ ràng kho lưu trữ của lớp cài đặt bằng cách triển khai phương thức
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }62
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }5
Khi một kho lưu trữ không được đặt cho một lớp cài đặt, thì
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }63 trong tệp cấu hình
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];7 sẽ được sử dụng
Tạo di chuyển cài đặt
Trước khi bạn có thể tải/cập nhật cài đặt, bạn sẽ phải di chuyển chúng. Mặc dù điều này thoạt nghe có vẻ hơi lạ, nhưng nó khá hợp lý. Bạn muốn có một số cài đặt mặc định để bắt đầu khi tạo một ứng dụng mới. Và điều gì sẽ xảy ra nếu chúng ta thay đổi thuộc tính của lớp cài đặt?
Đó là lý do tại sao gói yêu cầu di chuyển mỗi khi bạn thay đổi/tạo cấu trúc lớp cài đặt của mình. Các lần di chuyển này sẽ chạy bên cạnh các lần di chuyển cơ sở dữ liệu Laravel thông thường và chúng tôi đã thêm một số công cụ để viết chúng nhanh nhất có thể
Tạo di chuyển cài đặt hoạt động giống như bạn tạo di chuyển cơ sở dữ liệu thông thường. Bạn có thể chạy lệnh sau
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }7
Thao tác này sẽ thêm một lần di chuyển vào thư mục
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }65
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }7
Chúng tôi chưa thêm phương thức
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }66 nhưng có thể thêm phương pháp này nếu muốn. Trong phương pháp
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }67, bạn có thể thay đổi dữ liệu cài đặt trong kho lưu trữ khi di chuyển. Có một vài thao tác mặc định được hỗ trợ
Thêm thuộc tính
Bạn có thể thêm thuộc tính vào nhóm cài đặt như vậy
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }8
Chúng tôi đã thêm thuộc tính
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }68 vào nhóm
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];4 đang được sử dụng bởi
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];3. Bạn phải luôn đặt giá trị mặc định cho cài đặt mới tạo. Trong trường hợp này, đây là múi giờ
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }71
Nếu thuộc tính trong lớp cài đặt là nullable, thì có thể đặt giá trị mặc định là
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }72
Đổi tên thuộc tính
Có thể đổi tên thuộc tính
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }9
Bạn cũng có thể di chuyển một thuộc tính sang một nhóm khác
class GeneralSettingsController { public function update[ GeneralSettingsRequest $request, GeneralSettings $settings ]{ $settings->site_name = $request->input['site_name']; $settings->site_active = $request->input['site_active']; $settings->save[]; return redirect[]->back[]; } }0
Cập nhật một thuộc tính
Có thể cập nhật nội dung của một thuộc tính
class GeneralSettingsController { public function update[ GeneralSettingsRequest $request, GeneralSettings $settings ]{ $settings->site_name = $request->input['site_name']; $settings->site_active = $request->input['site_active']; $settings->save[]; return redirect[]->back[]; } }1
Như bạn có thể thấy, phương thức này lấy một bao đóng làm đối số, cho phép cập nhật một giá trị dựa trên giá trị cũ của nó
Xóa một thuộc tính
class GeneralSettingsController { public function update[ GeneralSettingsRequest $request, GeneralSettings $settings ]{ $settings->site_name = $request->input['site_name']; $settings->site_active = $request->input['site_active']; $settings->save[]; return redirect[]->back[]; } }2
Hoạt động trong nhóm
Khi bạn đang làm việc trên một lớp cài đặt lớn với nhiều thuộc tính, có thể hơi cồng kềnh khi luôn phải thêm nhóm cài đặt vào trước. Đó là lý do tại sao bạn cũng có thể thực hiện các thao tác trong một nhóm cài đặt
class GeneralSettingsController { public function update[ GeneralSettingsRequest $request, GeneralSettings $settings ]{ $settings->site_name = $request->input['site_name']; $settings->site_active = $request->input['site_active']; $settings->save[]; return redirect[]->back[]; } }3
thuộc tính gõ
Có thể tạo một lớp cài đặt với các loại PHP thông thường
class GeneralSettingsController { public function update[ GeneralSettingsRequest $request, GeneralSettings $settings ]{ $settings->site_name = $request->input['site_name']; $settings->site_active = $request->input['site_active']; $settings->save[]; return redirect[]->back[]; } }4
Bên trong gói sẽ chuyển đổi các loại này thành JSON và lưu chúng như vậy trong kho lưu trữ. Nhưng còn các loại như
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }73 và
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }74 hoặc các loại do chính bạn tạo ra thì sao?
Đó là lý do tại sao bạn có thể chỉ định các diễn viên trong gói này. Có hai cách để xác định các phôi này. địa phương hoặc toàn cầu
diễn viên địa phương
Các diễn viên cục bộ hoạt động trên một lớp cài đặt cụ thể và phải được xác định cho từng thuộc tính
class GeneralSettingsController { public function update[ GeneralSettingsRequest $request, GeneralSettings $settings ]{ $settings->site_name = $request->input['site_name']; $settings->site_active = $request->input['site_active']; $settings->save[]; return redirect[]->back[]; } }5
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }75 có thể được sử dụng cho các thuộc tính có loại như
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }73,
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }77,
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }74 và
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }79. Bạn cũng có thể sử dụng một diễn viên đã được xây dựng. Nó trở nên tiện dụng khi bạn cần chuyển một số đối số bổ sung cho diễn viên
class GeneralSettingsController { public function update[ GeneralSettingsRequest $request, GeneralSettings $settings ]{ $settings->site_name = $request->input['site_name']; $settings->site_active = $request->input['site_active']; $settings->save[]; return redirect[]->back[]; } }6
Như bạn có thể thấy, chúng tôi cung cấp
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }00 cho diễn viên, để nó biết loại
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }73 nên sử dụng vì thuộc tính
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }02 không được nhập và diễn viên không thể suy ra loại sẽ sử dụng
Bạn cũng có thể cung cấp đối số cho một diễn viên mà không cần xây dựng nó
class GeneralSettingsController { public function update[ GeneralSettingsRequest $request, GeneralSettings $settings ]{ $settings->site_name = $request->input['site_name']; $settings->site_active = $request->input['site_active']; $settings->save[]; return redirect[]->back[]; } }7
diễn viên toàn cầu
Các diễn viên cục bộ rất phù hợp để xác định các loại cho các thuộc tính cụ thể của lớp cài đặt. Nhưng có rất nhiều công việc để xác định một diễn viên cục bộ cho từng loại được sử dụng thường xuyên như một
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }73. Diễn viên toàn cầu cố gắng đơn giản hóa quy trình này
Bạn có thể xác định các diễn viên toàn cầu trong mảng
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }04 của cấu hình gói. Chúng tôi đã thêm một số diễn viên mặc định vào cấu hình có thể hữu ích
class GeneralSettingsController { public function update[ GeneralSettingsRequest $request, GeneralSettings $settings ]{ $settings->site_name = $request->input['site_name']; $settings->site_active = $request->input['site_active']; $settings->save[]; return redirect[]->back[]; } }8
Một diễn viên toàn cầu có thể làm việc trên
- một loại cụ thể [______105]
- một loại thực hiện một giao diện [
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }
06] - một loại kéo dài từ một lớp khác [
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }
07]
Trong lớp cài đặt của bạn, khi bạn sử dụng thuộc tính
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }73 [triển khai
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }09], bạn không còn phải xác định các diễn viên cục bộ
class GeneralSettingsController { public function update[ GeneralSettingsRequest $request, GeneralSettings $settings ]{ $settings->site_name = $request->input['site_name']; $settings->site_active = $request->input['site_active']; $settings->save[]; return redirect[]->back[]; } }9
Gói sẽ tự động tìm diễn viên và sẽ sử dụng nó để chuyển đổi các loại giữa lớp cài đặt và kho lưu trữ
thuộc tính gõ
Có khá nhiều tùy chọn để gõ thuộc tính. Bạn có thể gõ chúng trong PHP
composer require spatie/laravel-settings0
Hoặc bạn có thể sử dụng docblocks
composer require spatie/laravel-settings1
Docblocks có thể rất hữu ích để gõ các mảng đối tượng
composer require spatie/laravel-settings2
Thuộc tính khóa
Khi bạn muốn tắt khả năng cập nhật giá trị của cài đặt, bạn có thể thêm khóa vào cài đặt đó
composer require spatie/laravel-settings3
Hiện tại không thể cập nhật giá trị của
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }02. Khi cố gắng ghi đè lên
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }02 và lưu cài đặt, gói sẽ tải giá trị cũ của
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }02 từ kho lưu trữ và có vẻ như không có gì xảy ra
Bạn cũng có thể khóa nhiều cài đặt cùng một lúc
composer require spatie/laravel-settings4
Bạn có thể nhận được tất cả các cài đặt bị khóa
composer require spatie/laravel-settings5
Cài đặt mở khóa có thể được thực hiện như vậy
composer require spatie/laravel-settings6
Kiểm tra xem một cài đặt hiện đang bị khóa có thể được thực hiện như vậy
composer require spatie/laravel-settings7
Việc kiểm tra xem một cài đặt hiện có được mở khóa hay không có thể được thực hiện như vậy
composer require spatie/laravel-settings8
Mã hóa thuộc tính
Một số thuộc tính trong lớp cài đặt của bạn có thể được bảo mật, chẳng hạn như khóa API. Có thể mã hóa một số thuộc tính của bạn, vì vậy sẽ không thể đọc chúng khi dữ liệu kho lưu trữ của bạn bị xâm phạm
Việc thêm mã hóa vào các thuộc tính của lớp cài đặt của bạn có thể được thực hiện như vậy. Bằng cách thêm phương thức tĩnh
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }13 vào lớp cài đặt của bạn và liệt kê tất cả các thuộc tính sẽ được mã hóa
composer require spatie/laravel-settings9
Sử dụng mã hóa trong di chuyển
Việc tạo và cập nhật các thuộc tính được mã hóa trong quá trình di chuyển hoạt động hơi khác một chút so với các thuộc tính không được mã hóa
Thay vì gọi phương thức
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }14 để tạo thuộc tính mới, bạn nên sử dụng phương thức
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }15
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations" php artisan migrate0
Điều tương tự cũng xảy ra với phương thức
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }16, phương thức này sẽ được thay thế bằng phương pháp
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }17
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations" php artisan migrate1
Bạn có thể mã hóa thuộc tính không mã hóa trong quá trình di chuyển
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations" php artisan migrate2
Hoặc làm cho một thuộc tính được mã hóa không được mã hóa
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations" php artisan migrate3
Tất nhiên, bạn có thể sử dụng các phương pháp này khi sử dụng hoạt động di chuyển
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }18
Làm giả các lớp cài đặt
Trong các thử nghiệm, đôi khi mong muốn rằng một số lớp cài đặt có thể được sử dụng nhanh chóng với các giá trị khác với giá trị mặc định mà bạn đã viết trong quá trình di chuyển của mình. Đó là lý do tại sao bạn có thể giả mạo cài đặt. Các lớp cài đặt giả mạo sẽ được đăng ký trong vùng chứa. Và bạn có thể ghi đè lên một số hoặc tất cả các thuộc tính trong lớp cài đặt
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations" php artisan migrate4
Bây giờ, khi lớp cài đặt
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }19 được đưa vào đâu đó trong ứng dụng của bạn, thuộc tính
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }02 sẽ là
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }21
Cài đặt bộ đệm
Phải mất một khoảng thời gian nhỏ để tải lớp cài đặt từ kho lưu trữ. Khi bạn có nhiều lớp cài đặt, lượng thời gian nhỏ được thêm vào này có thể tăng nhanh chóng ngoài tầm kiểm soát. Gói này có hỗ trợ tích hợp để lưu trữ các cài đặt được lưu trữ bằng bộ đệm của Laravel
Trước tiên, bạn nên kích hoạt bộ đệm trong tệp cấu hình
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];7
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations" php artisan migrate5
Chúng tôi khuyên bạn nên kích hoạt bộ nhớ đệm trong quá trình sản xuất bằng cách thêm
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }23 vào tệp
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }24 của mình. Cũng có thể xác định nơi lưu trữ cho bộ đệm, đây phải là một trong những nơi lưu trữ bạn đã xác định trong tệp cấu hình
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }25. Nếu không có cửa hàng nào được xác định, kho lưu trữ bộ nhớ cache mặc định sẽ được sử dụng. Để tránh xung đột trong bộ đệm, bạn cũng có thể xác định tiền tố sẽ được thêm vào mỗi mục nhập bộ đệm
Đó là nó. Gói hiện đủ thông minh để lưu trữ cài đặt vào lần đầu tiên chúng được tải. Bất cứ khi nào cài đặt được chỉnh sửa, gói sẽ làm mới cài đặt
Bạn luôn có thể xóa các cài đặt được lưu trong bộ nhớ cache bằng lệnh sau
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations" php artisan migrate6
Tự động phát hiện các lớp cài đặt
Mỗi lớp cài đặt bạn tạo phải được thêm vào mảng
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];8 trong tệp cấu hình
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];7. Khi bạn có nhiều cài đặt, điều này có thể nhanh chóng bị lãng quên
Đó là lý do tại sao cũng có thể tự động phát hiện các lớp cài đặt. Gói sẽ xem qua ứng dụng của bạn và cố gắng khám phá các lớp cài đặt. Bạn có thể chỉ định các đường dẫn sẽ được tìm kiếm trong mảng cấu hình
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }28. Theo mặc định, đây là đường dẫn ứng dụng của ứng dụng
Cài đặt tự động phát hiện yêu cầu thêm thời gian trước khi ứng dụng của bạn được khởi động. Đó là lý do tại sao có thể cache chúng bằng lệnh sau
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations" php artisan migrate7
Bạn có thể xóa bộ đệm này bằng cách chạy
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations" php artisan migrate8
Viết bánh xe của riêng bạn
Caster là một lớp triển khai giao diện
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }29
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations" php artisan migrate9
Một caster đã tạo có thể được sử dụng cho các cast cục bộ và toàn cầu, nhưng có một số khác biệt nhỏ giữa chúng. Gói sẽ luôn cố gắng thêm loại thuộc tính mà nó đang truyền. Loại này là một chuỗi lớp và sẽ được cung cấp làm đối số đầu tiên khi xây dựng caster. Khi nó không thể suy ra loại,
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }72 sẽ được sử dụng làm đối số đầu tiên
Một ví dụ về bánh xe như vậy với một loại được đưa vào là một
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }31 được đơn giản hóa
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="settings"0
Trên đây là một caster cho gói spatie/data-transfer-object, bên trong hàm tạo của nó, loại sẽ là một lớp DTO cụ thể, ví dụ:
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }32. Trong phương thức
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }33, caster sẽ xây dựng một
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }32 với các thuộc tính của kho lưu trữ. Caster nhận một
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }32 dưới dạng tải trọng trong phương thức
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }36 và chuyển đổi nó thành một mảng để lưu trữ an toàn trong kho lưu trữ
diễn viên địa phương
Khi sử dụng diễn viên cục bộ, có một vài khả năng khác nhau để suy ra loại
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="settings"1
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="settings"2
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="settings"3
Trong trường hợp cuối cùng đó. theo định nghĩa rõ ràng, có thể cung cấp các đối số bổ sung sẽ được chuyển đến hàm tạo
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="settings"4
Mặc dù trong trường hợp này, việc xây dựng caster trong lớp cài đặt có thể dễ đọc hơn
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="settings"5
diễn viên toàn cầu
Khi sử dụng các phép truyền toàn cầu, gói sẽ lại cố gắng suy ra loại thuộc tính mà nó đang truyền. Trong trường hợp này, nó chỉ có thể sử dụng loại thuộc tính hoặc suy ra loại docblock của thuộc tính
Một diễn viên toàn cầu phải được định cấu hình trong tệp cấu hình
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];7 và luôn có một [bộ] [các] loại cụ thể mà nó hoạt động trên đó. Các loại này có thể là một lớp cụ thể, một nhóm các lớp triển khai một giao diện hoặc một nhóm các lớp mở rộng từ một lớp khác
Một ví dụ điển hình ở đây là
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }75 mà chúng tôi đã thêm theo mặc định trong cấu hình. Nó được định nghĩa trong cấu hình như vậy
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="settings"6
Bất cứ khi nào gói phát hiện loại
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }74,
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }79,
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }73 hoặc
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }77 là loại thuộc tính của một trong các thuộc tính của lớp cài đặt. Nó sẽ sử dụng
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }75 như một caster. Điều này bởi vì
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }74,
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }79,
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }73 và
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }77 đều triển khai
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }09. Khóa đã được sử dụng trong
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];7 để đại diện cho dàn diễn viên
Loại được đưa vào caster sẽ là loại thuộc tính. Vì vậy, giả sử bạn có một thuộc tính với loại
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }73 trong lớp cài đặt của mình. Khi truyền thuộc tính này,
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }75 sẽ nhận được
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }52 dưới dạng loại
kho lưu trữ
Có hai loại kho lưu trữ được bao gồm trong gói, kho lưu trữ
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }38 và
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }37. Bạn có thể tạo nhiều kho lưu trữ cho một loại trong tệp cấu hình
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }55. Và mỗi kho lưu trữ có thể được cấu hình
kho cơ sở dữ liệu
Kho lưu trữ cơ sở dữ liệu có hai tùy chọn cấu hình tùy chọn
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }
56 mô hình Eloquent được sử dụng để tải/lưu các thuộc tính vào cơ sở dữ liệuclass GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }
57 bảng được sử dụng trong cơ sở dữ liệuclass GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }
58 kết nối để sử dụng khi tương tác với cơ sở dữ liệu
Kho lưu trữ Redis
Kho lưu trữ Redis cũng có hai tùy chọn cấu hình tùy chọn
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }
59 một tiền tố tùy chọn sẽ được thêm vào trước các phímclass GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }
58 kết nối để sử dụng khi tương tác với Redis
Bộ nhớ đệm
Có thể thêm cấu hình bộ nhớ đệm tùy chỉnh cho mỗi kho lưu trữ, bằng cách thêm cấu hình bộ đệm giống như cấu hình mặc định vào cấu hình kho lưu trữ của bạn trong tệp cấu hình
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];7
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="settings"7
Tạo loại kho lưu trữ của riêng bạn
Có thể tạo các loại kho lưu trữ của riêng bạn. Một kho lưu trữ là một lớp thực hiện
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }62
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="settings"8
Tất cả các chức năng này phải được triển khai để tương tác với loại bộ nhớ bạn đang sử dụng. Các tham số
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }63 là các giá trị thô [
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }64,
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }65,
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }66,
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }67,
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }68]. Trong các loại kho lưu trữ
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }37 và
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }38, các giá trị thô này được chuyển đổi thành JSON. Nhưng điều này là không bắt buộc
Cần phải trả lại các giá trị thô một lần nữa trong các phương thức
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }71 và
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }72
Mỗi hàm tạo của kho lưu trữ sẽ nhận được một mảng
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }73 mà người dùng xác định cho kho lưu trữ trong tệp cấu hình
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];7 của ứng dụng. Có thể thêm các phụ thuộc khác vào hàm tạo. Chúng sẽ được đưa vào khi kho lưu trữ được tạo
Làm mới cài đặt
Bạn có thể làm mới các giá trị và thuộc tính bị khóa trong lớp cài đặt. Điều này có thể hữu ích nếu bạn thay đổi điều gì đó trong kho lưu trữ của mình và muốn thấy điều đó được phản ánh trong cài đặt của bạn
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="settings"9
Bạn chỉ nên làm mới cài đặt khi giá trị kho lưu trữ đã thay đổi khi lớp cài đặt đã được tải
Sự kiện
Gói sẽ phát ra một loạt các sự kiện khi tải/lưu các lớp cài đặt
class GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }
75 bất cứ khi nào cài đặt được tải từ kho lưu trữ nhưng chưa được chèn vào lớp cài đặtclass GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }
76 sau khi cài đặt được tải vào lớp cài đặtclass GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }
77 bất cứ khi nào cài đặt được lưu vào kho lưu trữ nhưng chưa được truyền hoặc mã hóaclass GeneralSettingsController { public function show[GeneralSettings $settings]{ return view['settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]]; } }
78 sau khi cài đặt được lưu trữ trong kho lưu trữ
thử nghiệm
return [ /* * Each settings class used in your application must be registered, you can * put them [manually] here. */ 'settings' => [ ], /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path['settings'], ] /* * When no repository was set for a settings class the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env['SETTINGS_CACHE_ENABLED', false], 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't a default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app[]->path[], ], /* * Automatically discovered settings classes can be cached so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => storage_path['app/laravel-settings'], ];0
Nhật ký thay đổi
Vui lòng xem CHANGELOG để biết thêm thông tin về những thay đổi gần đây
Đóng góp
Vui lòng xem ĐÓNG GÓP để biết chi tiết
Bảo vệ
Nếu bạn tìm thấy một lỗi liên quan đến bảo mật, vui lòng gửi mail security@spatie. thay vì sử dụng trình theo dõi vấn đề