【问题标题】:Route isn't redirecting me to the correct page. It just shows me the same page but the url changesRoute 没有将我重定向到正确的页面。它只是向我显示相同的页面,但网址发生了变化
【发布时间】:2021-11-25 20:13:55
【问题描述】:

我已经制作了一个博客模板,但在制作路线时遇到了麻烦。当我点击链接时,它应该将我重定向到文章页面。相反,它只是将我重定向到相同的页面/相同的确切位置。 这是我的代码:

<section id="about" class="ts-block" >
    <div id="cards_landscape_wrap-2">
    <div class="container">
        <div class="row">
        @foreach($blogs as $blog)
            <div class="col-lg-4">
                <a href={{route("blogs.show",$blog->slug)}}>
                    <div class="card-flyer"  >
                        <div class="text-box">
                            <div class="image-box ">
                                <img src="{{'/images/blogs/'.$blog->image}}" alt="{{$blog->title}}" class="img-fluid rounded mx-auto d-block" width="100%"/>
                            </div>
                            <div class="text-container">
                                <h6>{{$blog->title}}</h6>
                                <p style="max-width: 18rem;"class="ts-opacity__50">{{Str::limit($blog->description,100)}}</p>
                            </div>
                            <div class="card-footer">
                                        <span class="text-primary font-weight-bold">Read</span>
                            </div>
                        </div>
                    </div>
                </a>
            </div>
            @endforeach
        </div>
    </div>
    </div>
    </section>    

博客控制器

public function getArticles()
{
    return view('blog')->with([
        'blogs' => Blog::all(),
    ]);
}

路线

Route::get('/blog/', [App\Http\Controllers\BlogController::class, 'index'])->name('blog');
Route::get('blogs/','App\Http\Controllers\BlogController@getArticles')->name('blogs.show');
Route::get('/admin/blog', 'App\Http\Controllers\BlogController@getBlog')->name('admin.blog');
Route::resource('/blog','App\Http\Controllers\BlogController');

应该将我重定向到另一个页面的路线是带有BlogController@getArticles 的第二条路线。其他路线都很好,没有问题。

谁能告诉我我做错了什么?

【问题讨论】:

    标签: javascript php laravel laravel-5


    【解决方案1】:

    您的问题有点令人困惑,因为这是代码的预期结果。

    您正在调用“blogs.show”路由,该路由会将您重定向到博客页面。如果你想为每篇文章创建一个单独的页面,你可以在 laravel 中使用Dynamic Rounting

    你所要做的就是:

    在 web.php 中:

    Route::get('blogs/{slug}', 'App\Http\Controllers\BlogController@getArticle')->name('article.show');
    

    在你的控制器中:

    public function getArticles($slug)
    {
    
        $article = Blog::where('slug', $slug)->first();
        return view('article')->with([
            'article' => $article,
        ]);
    }
    

    之后,&lt;a href={{route("article.show",$blog-&gt;slug)}}&gt;&lt;/a&gt; 将起作用。

    【讨论】:

    • 好的。感谢您的解释。这实际上有效,但我添加了 'return view('article.show')'
    【解决方案2】:

    首先,改变你的路线:

    Route::get('/blogs/{slug}','App\Http\Controllers\BlogController@getArticles')->name('blogs.show');
    

    然后,在你的函数中:

    public function getArticles($slug)
    {
        return view('blog')->with([
            'blogs' => Blog::firstWhere('slug', $slug);
        ]);
    }
    

    【讨论】:

    • 它给了我'试图获得非对象的属性'slug'。它应该以分号还是逗号结尾? 'blogs' => Blog::firstWhere('slug', $slug),
    • 您是否在blog 模型public function getRouteKeyName(){ return 'slug'; } 中定义了路键? PS。它以分号结尾
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 2022-01-05
    相关资源
    最近更新 更多