【问题标题】:Laravel 5.2 - Update routes paths during executionLaravel 5.2 - 在执行期间更新路由路径
【发布时间】:2018-04-28 23:22:25
【问题描述】:

在 Laravel 5.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');
});
  • $GLOBALS['site_segment'] 和 $GLOBALS['is_cron'] 只有在 Console/Kernel.php::schedule() 上执行 cron 时才会设置

  • helpers.siteTemplate 只会初始化proviter。

  • Helpers_SiteTemplate::getSiteRoute 只会从 URL 或参数 var 中获取站点名称

这将从 url 正常工作,在本例中为:domain.tld/sitename/test,但从 cron 中,routes.php 将设置不带站点名称的 url,然后我需要再次设置路由。 在设置会话变量后,我尝试从 schedule() 中 require app_path('Http/routes.php');,但路由没有改变。

【问题讨论】:

  • 你不能为此使用会话或配置变量吗?
  • cron 执行类在 boostrap 之后执行,我确实使用会话变量来设置站点名称,并在 routes.php 中使用中间件从 url 中获取站点名称,但是在执行我想要的 cron 时为每个站点执行一次运行,为此我需要每次更新路由路径......但即使我再次调用 Route::get() 更改也不会保存:/
  • 我认为我们需要查看更多代码才能更好地理解您在说什么
  • 我确实更新了我的 OP,如果需要更多信息,请告诉我。谢谢!

标签: php laravel laravel-5.2


【解决方案1】:

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

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

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

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

谢谢大家。

【讨论】:

    【解决方案2】:

    对于 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);
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2018-09-12
        • 1970-01-01
        • 2016-08-30
        • 2016-12-01
        • 2016-06-24
        • 1970-01-01
        • 2016-03-30
        • 2023-04-06
        • 1970-01-01
        相关资源
        最近更新 更多