Laravel:将数据从数据库/控制器传递到RouteServiceProvider.php

问题描述

我的问题是,有没有可能将数据从数据库传递到RouteServiceProvider.PHP文件中。如果是这样,您能告诉我下面的代码是否正确。

我试图传递给RouteServiceProvider的地方是一个主题位置,以使主题支持更加整洁且易于维护。

我要实现的代码

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'));
    });
}

我的use文件位于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;

我的ThemeSettings模型

<?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'];
}

我的ThemeSettings控制器(ThemeSettingsController.PHP

<?PHP

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\ThemeSettings;

class ThemeSettingsController extends Controller
{
    public $theme_settings;
    /**
     * display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $theme_settings = ThemeSettings::get();
        return view('admin.settings.theme',compact('theme_settings'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request,$id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

就像我在上面说的那样,我不知道是否有可能,但是如果有可能,我所拥有的某些东西并没有按照应有的方式工作,我绝对可以使用一些帮助。

如果您能提供任何信息,我们将不胜感激!

解决方法

您无需在启动功能中定义theme_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'));
    });
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...