【问题标题】:Laravel routing not completingLaravel 路由未完成
【发布时间】:2016-05-26 15:13:20
【问题描述】:

我对 Laravel 有点陌生。

我已经创建了一个表单,提交它以进行授权,但随后(Firefox)告诉我路由永远不会完成。我知道登录成功了,因为我拦截了它。

这是我的 routes.php:

Route::get('/',function()
{
     return view('welcome');
})->name('home');


Route::get('/welcome', function () {
      return view('welcome');
});

Route::post('/signin',
    [
        'uses' =>'UserController@postSignIn',
        'as' => 'SignIn'
    ]);


Route::get('/dashboard',
    [
        'uses' => 'UserController@getDashboard',
        'as' => 'DashBoard',
        'middleware' => 'auth'
    ]);

Route::get('/logout',
    [
        'uses' => 'UserController@getLogout',
        'as' => 'Logout'
    ]);

这里是用户控制器:

 class UserController extends Controller
{

    public function postSignIn(Request $request)
    {
        $this->validate($request,
            [
                'email' => 'required | email',
                'password' => 'required'
            ]);

        if (Auth::attempt([ 'email' => $request['email'], 'password' =>$request['password'] ]) )
        {
            //exit("authorised");
            $message = "you are now logged in";
            return redirect()->route('DashBoard')->with(['successmessage' =>$message]);
        }
        else
        {
            $message = "username\password combination not correct";
            //exit('not - email = '.$request['email'].' password = '. $request['password']);
            return redirect()->back()->with(['errormessage' => $message] );
        }
    }

    public function getLogout()
    {
        Auth::logout();
        return redirect()->route('home');
    }

    public function getDashboard()
    {
       return redirect()->route('DashBoard');
    }
}

从注释掉的内容可以看出授权是OK的

但我是从 Firefox 获得的

页面未正确重定向 Firefox 检测到服务器正在以永远不会完成的方式重定向对该地址的请求。

【问题讨论】:

    标签: php routing laravel-5.2


    【解决方案1】:

    只需在你的回报中使用 to() 作为

    return redirect()->to('DashBoard')->with(['successmessage' =>$message]);

    【讨论】:

    • 我试过了,现在我得到“RouteCollection.php 第 161 行中的 NotFoundHttpException:”
    • 你的路线是仪表板,所以像这样使用它 return redirect()->to('dashboard')->with(['successmessage' =>$message]);
    • 使用 ->to('/dahsboard') 作为回报
    【解决方案2】:

    将此添加到您的路线

    Route::get('/signin',
    [
        'uses' =>'UserController@postSignIn',
        'as' => 'SignIn'
    ]);
    

    【讨论】:

    • 获取。这将在现有 POST 路由旁边定义 GET 路由。如果有人想知道这个答案有什么不同。
    • 你是对的。 GET 只会简单地“获取”到您的视图的路由......而 POST 是来自您视图上的表单的表单操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 2016-02-27
    • 2018-12-16
    • 2015-02-18
    • 2015-01-06
    相关资源
    最近更新 更多