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 三向应力状态求剪应力
  • ¥15 jupyter notebook如何添加libGL.so.1库
  • ¥20 easyPoi能否实现下拉多选或者复选框
  • ¥15 网桥在转发帧时,会变帧的源地址和目的地址吗?
  • ¥15 用Multisim设计汽车尾灯控制电路
  • ¥100 求用matlab求解上述微分方程的程序代码
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败