【问题标题】:Laravel base URL redirecting to /home for no reasonLaravel 基础 URL 无缘无故重定向到 /home
【发布时间】:2019-05-31 08:15:27
【问题描述】:

我正在学习 laravel,但我的基本 URL 有问题,即 http://localhost/

它不断重定向到http://localhost/home,这是基本身份验证路由,由于我没有登录,它会将我重定向到http://localhost/login

我希望 http://localhost/ 按原样重定向到 http://localhost/blog/posts

我这样做是因为将来基本 url 将重定向到另一个页面。在那之前,我想显示博客文章。

web.php

Route::get('/', [
    'as' => 'index',
    'uses' => 'HomeController@index',
]);

Route::get('/blog/posts', [
    'as' => 'blog',
    'uses' => 'BlogController@index'
]);

Auth::routes();

Route::get('/home', [
    'as' => 'home',
    'uses' => 'HomeController@home'
]);

HomeController.php

public function home()
{
    return view('home');
}

public function index()
{
    return view('blog');
}

我希望我已经足够清楚了,如果需要,我很乐意提供更多信息。

问题已解决:

注释或删除 HomeController.php 中的$this->middleware('auth'); 并将其添加到路由中:

Route::get('/home', [
    'as' => 'home',
    'uses' => 'HomeController@home'
])->middleware('auth');

【问题讨论】:

    标签: laravel laravel-routing laravel-authentication


    【解决方案1】:

    检查 HomeController __construct() function 内部是否没有 auth middleware,它会先尝试让您登录,然后继续检查索引功能。

    【讨论】:

    • 谢谢你说得有道理。我应该看到的,我猜我不小心。
    猜你喜欢
    • 1970-01-01
    • 2016-11-10
    • 2015-11-10
    • 2019-05-07
    • 2016-12-19
    • 2017-04-11
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多