Laravel:将数据从 Database/Controller 传递到 RouteServiceProvider.php

Laravel: Pass data from Database/Controller into 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'));
    });
}

我在 RouteServiceProvider.php 文件顶部的 use 语句:

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

我的主题设置控制器(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'));
    });
}