【问题标题】:Laravel Optional Route IssueLaravel 可选路由问题
【发布时间】:2020-02-08 15:58:23
【问题描述】:

我的可选路线如下所示:

Route::group(['prefix' => 'blog'], function ({

    Route::get('/', [
        'uses' => 'BlogController@showBlog',
        'as' => 'show.blog'
    ]);

    Route::get('{author}/{y?}/{m?}/{d?}/{title?}', [
        'uses' => 'BlogController@showDetailBlog',
        'as' => 'detail.blog'
    ]);

});

它的控制器看起来像这样:

public function showDetailBlog($author, $year = null, $month = null, $date = null, $title = null)
{
    $user = User::where('username', $author)->first();

    if(!$year && !$month && !$date && !$title) {
        return view('pages.blog.author', compact('user'));

    } else {
        $blog = Blog::where('user_id', $user->id)->whereYear('created_at', $year)
            ->whereMonth('created_at', $month)->whereDay('created_at', $date)
            ->where('title_uri', $title)->first();
        $relates = Blog::where('category_id', $blog->category_id)->orderByDesc('id')->get();

        $tgl = Carbon::parse($blog->created_at);
        $uri = route('detail.blog', ['author' => $user->username, 'y' => $tgl->format('Y'),
            'm' => $tgl->format('m'), 'd' => $tgl->format('d'),
            'title' => $blog->title_uri]);

        return view('pages.blog.detail', compact('user', 'blog', 'relates', 'uri'));
    }
}

当我用完整的参数请求它时:

/blog/author123/2019/10/10/lorem-ipsum-dolor-sit-amet

它将正确返回到详细博客视图。但是当我请求它时只使用作者参数,例如:

/blog/author123

不会返回作者的博客视图,总是返回一个空数组。

有人知道我错过了什么吗?

【问题讨论】:

  • 愚蠢的问题:为什么你在 URL 中有/blog/,但在路由中没有?你在一个路由组吗?路线和功能是正确的。但是,根据您提供的内容(除非您有路由组),这两个 URL 都应该失败并命中您的 404。
  • ya 在路由组中,前缀为“blog”
  • @Raul 好吧.. 如果你不介意,再次检查我的路线,我只是更新它:v
  • 你能开始调试以确保它正在输入那部分代码吗?您可能有另一个路由正在覆盖此路由并在另一个控制器中执行。我以前见过很多次。例如:你可能有一个路由 /blog/authhor/{something},Laravel 会先匹配它。
  • 你的群回调不正确,试试Route::group(['prefix' => 'blog'], function () {

标签: php laravel routes optional-parameters


【解决方案1】:

天啊!!有一条路线会覆盖它,这就是它返回空数组的原因。所以,我只需要替换这个 get.title.blog 路由:

Route::group(['prefix' => 'blog'], function () {

    // other blog route

    Route::get('{title}', [
       'uses' => 'BlogController@getTitleBlog',
       'as' => 'get.title.blog'
    ]);

    Route::get('{author}/{y?}/{m?}/{d?}/{title?}', [
       'uses' => 'BlogController@showDetailBlog',
       'as' => 'detail.blog'
    ]);

});

进入这个:

Route::get('title/{title}', [
   'uses' => 'BlogController@getTitleBlog',
   'as' => 'get.title.blog'
]);

我是多么愚蠢,哈哈。但是我无法意识到没有你的帮助,尤其是@Raul tysm dude :D

【讨论】:

  • 是的,你也可以删除控制器中的 else (因为在 if 语句中你已经有一个 return)
【解决方案2】:
public function showDetailBlog($author, $year = null, $month = null, $date = null, $title = null)
    {
        $user = User::where('username', $author)->first();

        if (!$user) abort(404);

        if (is_null($year) && is_null($month) && is_null($date) && is_null($title)) {
            return view('pages.blog.author', compact('user'));
        }

        $blog = Blog::where('user_id', $user->id);

        if (!is_null($year)) {
            $blog = $blog->whereYear('created_at', $year);
        }
        if (!is_null($month)) {
            $blog = $blog->whereMonth('created_at', $month);
        }
        if (!is_null($date)) {
            $blog = $blog->whereDay('created_at', $date);
        }
        if (!is_null($title)) {
            $blog = $blog->where('title_uri', $title);
        }
        $blog = $blog->first();


        $relates = Blog::where('category_id', $blog->category_id)
            ->where('id', '!=', $blog->id)
            ->orderByDesc('id')->get();

        $tgl = Carbon::parse($blog->created_at);
        $uri = route('detail.blog', ['author' => $user->username, 'y' => $tgl->format('Y'),
            'm' => $tgl->format('m'), 'd' => $tgl->format('d'),
            'title' => $blog->title_uri]);

        return view('pages.blog.detail', compact('user', 'blog', 'relates', 'uri'));
    }

【讨论】:

  • hmm thx,但我刚刚尝试过,它仍然返回一个空数组..
【解决方案3】:

试试这个

public function showDetailBlog($author, $year = null, $month = null, $date = null, $title = null)
{
    dd($author);
}

检查您是否收到author123

然后尝试添加这个:

Route::get('{author}', [
        'uses' => 'BlogController@showDetailBlog',
        'as' => 'detail.blog'
    ]);

在你的路由中查看你的控制器是否仍然被该路由击中

另一方面,你可以使用 Laravel 模型绑定!

在您的 RouteServiceProvider 中

public function boot()
{
    parent::boot();

    Route::bind('author', function ($value) {
        return App\User::where('username', $value)->first() ?? abort(404);
    });
}
public function showDetailBlog(User $author, $year = null, $month = null, $date = null, $title = null)
{
    dd($author); // You will have your author instance !
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-14
    • 2018-11-20
    • 1970-01-01
    • 2017-03-13
    • 2012-08-16
    相关资源
    最近更新 更多