laravel 重定向太多次laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47073599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:54:08  来源:igfitidea点击:

redirect too many times laravel

laravellaravel-5

提问by test1321

I try to use redirect->action It error too many redirect in my Controller

我尝试使用redirect->action 它在我的控制器中错误太多重定向

try {
  //If check urL category null
  if (is_null($category)){
      Log::error("[Front] MenuController@menu : notfound public category ");

      //error redirect
       return redirect()->action('Front\HomeSlideviewController@index', $url);
  }
} catch (\Exception $e) {
    return 'error';
}   

here is my web.php

这是我的 web.php

  Route::get('/{url?}', 'MenuController@menu');
  Route::get('/{name?}', 'HomeSlideviewController@index')->name('promotiondetail');

I try to make a fucntion if Url is empty use redirect action

如果 Url 为空,我会尝试使用重定向操作

回答by lchachurski

Both of your routes are identical and only the first one is matched.

你的两条路线是相同的,只有第一个是匹配的。

Please note that, when redirecting to action, Laravel is resolving your action to route anyway, so it is the same as redirecting to route name (which is more bulletproof). By the way, the second parameter should be an array.

请注意,当重定向到 action 时,Laravel 会解析您的 action 以进行路由,因此它与重定向到路由名称相同(更防弹)。顺便说一下,第二个参数应该是一个数组。

return redirect()->action('Front\HomeSlideviewController@index', $url);

To do what you want, you need one catchAllaction and return different responses based on your logic:

为了做你想做的事,你需要一个catchAll动作并根据你的逻辑返回不同的响应:

/**
 * @param $string
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
 */
public function catchAllAction($string)
{
    $page = Page::whereHas('translations', function ($query) use ($string) {
        $query->where('locale', App::getLocale())->where('slug', $string);
    })->first();

    if ($page) {
        return $this->showPage($page);
    }

    $news = News::whereHas('translations', function ($query) use ($string) {
        $query->where('locale', App::getLocale())->where('slug', $string);
    })->first();

    if ($news) {
        return $this->showSingleNews($news);
    }

    throw new NotFoundHttpException('This page does not exist');
}

回答by jigs_

You have route conflict, it will call first route every time. try to change your route.

你有路由冲突,它每次都会调用第一条路由。尝试改变你的路线。