Laravel:将数据从数据库/控制器传递到RouteServiceProvider.php
我的问题是,是否可以在有控制器或没有控制器的情况下将数据从数据库传递到RouteServiceProvider.php文件中。如果是这样,你能告诉我下面的代码是否正确吗 我试图传递给RouteServiceProvider的是一个主题位置,以使主题支持更干净、更易于维护 我正在尝试实现的代码:Laravel:将数据从数据库/控制器传递到RouteServiceProvider.php,php,laravel,laravel-routing,laravel-8,illuminate-container,Php,Laravel,Laravel Routing,Laravel 8,Illuminate Container,我的问题是,是否可以在有控制器或没有控制器的情况下将数据从数据库传递到RouteServiceProvider.php文件中。如果是这样,你能告诉我下面的代码是否正确吗 我试图传递给RouteServiceProvider的是一个主题位置,以使主题支持更干净、更易于维护 我正在尝试实现的代码: public function boot() { $this->configureRateLimiting(); $this->routes(function () {
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
foreach ($theme_settings as $key=>$setting){
Route::middleware('web')
->group(base_path($setting->location . $setting->name . 'route.php'));
}
Route::middleware('web')
->group(base_path(env('THEME_DIR') . env('THEME_NAME') . '/route.php'));
});
}
我的使用RouteServiceProvider.php
文件顶部的语句:
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
use App\Models\ThemeSettings;
use App\HTTP\Controllers\ThemeSettingsController;
我的主题设置模型
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ThemeSettings extends Model
{
use HasFactory;
protected $fillable = ['name', 'description','author','location'];
}
您没有在启动函数中定义主题设置变量,请按如下方式执行:
public function boot()
{
$theme_settings = ThemeSettings::get();
$this->configureRateLimiting();
$this->routes(function ()use($theme_settings) {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
foreach ($theme_settings as $key=>$setting){
Route::middleware('web')
->group(base_path($setting->location . $setting->name . 'route.php'));
}
Route::middleware('web')
->group(base_path(env('THEME_DIR') . env('THEME_NAME') . '/route.php'));
});
}
有什么问题?有什么错误?@babak我得到了ErrorException未定义的变量:theme\u settings仍然在抛出未定义的变量:theme\u settings
public function boot()
{
$theme_settings = ThemeSettings::get();
$this->configureRateLimiting();
$this->routes(function ()use($theme_settings) {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
foreach ($theme_settings as $key=>$setting){
Route::middleware('web')
->group(base_path($setting->location . $setting->name . 'route.php'));
}
Route::middleware('web')
->group(base_path(env('THEME_DIR') . env('THEME_NAME') . '/route.php'));
});
}