douduan7295 2018-01-17 09:42
浏览 34
已采纳

有没有办法忽略子域路由中的子域值

According to Lavarel documentation, if I use subdomain routing for multiple subdomains, I have to pass the subdomain as the first argument of callbacks functions and controllers methods:

Route::domain('{sub}.example.org')->group(function () {
    Route::get('a', function ($sub) { /* ... */ });
    Route::get('b', function ($sub) { /* ... */ });
    Route::get('c/{c}', function ($sub, $c) { /* ... */ });
    Route::get('d/{d}', function ($sub, $d) { /* ... */ });
});

In other words, I have to carry the $sub variable everywhere. Assuming I don't care about its value, can I avoid this and just do something like this(this does not work for multiple argument):

Route::domain('{sub}.example.org')->group(function () {
    Route::get('a', function () { /* ... */ });
    Route::get('b', function () { /* ... */ });
    Route::get('c/{c}', function ($c) { /* ... */ });
    Route::get('d/{d}', function ($d) { /* ... */ });
});

If I do this, $c and $d will be the value of the subdomain.

Assuming I don't care about the subdomain value and I have many routes, is there a way to ignore it?

  • 写回答

1条回答 默认 最新

  • doulutian4843 2018-01-17 14:22
    关注

    We had a similar thing with a dynamical prefix. Our solution was:

    First we unset the parameter if it existed in the "App/Http/Controllers/Controller.php", which is the class your controllers most likely extend.

    public function callAction($method, $parameters)
    {
        if (isset($parameters['clientident'])) {
            unset($parameters['clientident']);
        }
    
        return parent::callAction($method, $parameters);
    }
    

    This will unset the parameter for every function call in every controller and should do away with your problem.

    But because we needed it in our route() function, we created a "App/Http/helpers.php" file for

    function route($name, $parameters = [], $absolute = true)
    {
        if (!isset($parameters['clientident'])) {
            // If the given value is not an array, wrap it in one.
            $parameters = Arr::wrap($parameters);
            if (Auth::check()) {
                $temp = ['clientident' => Auth::user()->getClientIdent()];
            }
            else if (request()->clientident) {
                $temp = ['clientident' => request()->clientident];
            }
            else {
                $temp = ['clientident' => 'general'];
            }
            $parameters = array_merge($parameters, $temp);
        }
        return app('url')->route($name, $parameters, $absolute);
    }
    

    and added the new helpers file in the "bootstrap/autoload.php"

    require __DIR__.'/../app/Http/helpers.php';
    require __DIR__.'/../vendor/autoload.php';         << this line was already here
    

    This works because Taylor wrote an check in front of every helper function if it is already set, so it's pretty easy to override them

    if (! function_exists('route')) {
        function route($name, $parameters = [], $absolute = true)
        {
            return app('url')->route($name, $parameters, $absolute);
        }
    }
    

    is the code in the helpers.php in the Illuminate/Foundation directory.

    I guess the route function has to be handled a little bit different in your case. You should be able to get the subdomain with domain helper functions and add it there as parameter back again, which is most likely much easier than our if elseif else case above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀