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 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试