phplaravellaravel-routinglaravel-8illuminate-container

Laravel: Pass data from Database/Controller into RouteServiceProvider.php


my question is if it is possible to pass data into the RouteServiceProvider.php file from the database with or without the controller. If so, could you tell me if my code below is correct.

What I am trying to pass into my RouteServiceProvider is a theme location to make theme support cleaner and easier to maintain.

The code I am trying to implement:

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

My use statements at the top of the RouteServiceProvider.php file:

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;

My ThemeSettings Model

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

My ThemeSettings Controller (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)
    {
        //
    }
}

Like I said above, I don't know if it is possible but if it is, something I have isn't working the way it should and I could most definitely use some help.

If you could provide me any information, it is greatly appreciated!


Solution

  • you don't define theme_settings variable in boot function do it like this:

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