Laravel 5.2 - 在执行期间更新路由路径

Laravel 5.2 - Update routes paths during execution

在 Laravel 5.2 中,我试图在执行期间更新路由路径,因为我有多个站点 运行,对于 cron 执行,我需要更新每个站点的路由路径。

我试着只用

app('url')->forceRootUrl('https://domain/sitename')

但是使用这种方法 asset() 函数将使用错误的 link。

我尝试要求 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 将被设置 urls 而没有站点名称,然后我需要重新设置路由。 设置会话变量后,我尝试从 schedule() require app_path('Http/routes.php');,但路线没有改变。

我终于成功地找到了解决这个问题的方法,所以对于未来的搜索者来说这可能会有所帮助。

如果你像我一样因为多站点的 cron 操作需要重建 ROUTES laravel 或者其他原因,我终于用这段代码做到了:

// 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']);

如果我能提供更多信息,请联系我。

谢谢大家。

答案已过时,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);

我将我的项目更新为 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'));