如何解决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'));
});
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。