在Laravel5.2中,我试图在执行期间更新路由路径,因为我有多站点运行,而对于cron执行,我需要更新每个站点的路由路径。
我试着用
app('url')->forceRootUrl('https://domain/sitename')但是使用这种方法,asset()函数将使用错误的链接。
我试图要求routes.php文件重新构建路径,但显然没有保存更改。
有什么想法吗?
谢谢
routes.php:
Route::group([
'prefix' => Helpers_SiteTemplate::getSiteRoute($GLOBALS['site_segment'], $GLOBALS['is_cron']),
'middleware' => ['helpers.siteTemplate'],
], function () {
Route::get('test', 'Admin\HomeController@debugging');
});在本例中,这将在url中正常工作: domain.tld/ sitename /test,但是在cron中,routes.php将在没有站点名的情况下设置url,然后我需要再次设置路由。在设置会话vars之后,我尝试从schedule()开始require app_path('Http/routes.php');,但是路由没有改变。
发布于 2017-11-28 21:08:14
我终于成功地在代码中找到了解决方案,因此对于未来的搜索者来说,这可能是有帮助的。
如果由于多站点laravel中的cron操作或其他原因,您需要重建像我这样的路线,那么我最终使用了以下代码:
// Get the router facade from anyware
$router = \Illuminate\Support\Facades\Route::getFacadeRoot();
// Clear the current routes by setting a empty route collection
$routes = new \Illuminate\Routing\RouteCollection;
$router->setRoutes($routes);
// Get the route service provider
$routeserviceprovider = new \App\Providers\RouteServiceProvider(app());
// Call the map function from the provider, this will remap all routes from the app\Http\routes.php
app()->call([$routeserviceprovider, 'map']);如果我能帮助进一步的信息,请联系我。
谢谢大家。
发布于 2019-01-03 09:58:42
对Laravel 5.7来说,答案已经过时了:
$router = app('router');
// Clear the current routes by setting a empty route collection
$routes = new \Illuminate\Routing\RouteCollection;
$router->setRoutes($routes);
// Get the route service provider
$routeserviceprovider = app()->getProvider(RouteServiceProvider::class);
// Call the map function from the provider, this will remap all routes from the app\Http\routes.php
app()->call([$routeserviceprovider, 'map']);
$routes = Route::getRoutes();
$routes->refreshNameLookups();
$routes->refreshActionLookups();
$router->setRoutes($routes);发布于 2021-03-30 20:18:13
我将我的项目更新为Laravel 8.33,经过几个小时的测试,这对我起了作用:
// clear routes
$router = \Illuminate\Support\Facades\Route::getFacadeRoot();
$routes = new \Illuminate\Routing\RouteCollection;
$router->setRoutes($routes);
// load routes
Route::middleware('web')
->namespace('App\Http\Controllers')
->group(base_path('routes/web.php'));https://stackoverflow.com/questions/47312419
复制相似问题