首页 文章

Laravel登录重定向不起作用

提问于
浏览
0

我已经制作了登录/登记表格 . 注册工作正常,但登录重定向不起作用 . 我的控制器中有以下功能:

public function doLogin() {
    $credentials = [
        'email' => Input::get('email'),
        'password' => Input::get('password')
    ];

    if (Auth::attempt($credentials)) {
        return Redirect::to('/');
    } else {
        dd('error');
    }
}

和routes.php

Route::resource('car', 'CarController');
Route::get('users', 'UserController@index');
Route::post('users/register', array('uses' => 'UserController@store'));
Route::post('users/signin', array('uses' => 'UserController@doLogin'));
Route::get('users/logout', array('uses' => 'UserController@doLogout'));
Route::get('/', 'CarController@index');

CarController

public function index() {
        $cars = DB::select('select * from cars');
        $result = DB::select('select c.*, i.sgs, i.tpl, i.kasko, i.inter_permis from cars as c left join insur_docs as i  on i.car_id = c.id');
        $date = Carbon::now();
        $limit_date = Carbon::now()->addMonths(1);
        return View::make('pages.index', array(
                    'cars' => $cars,
                    'result' => $result,
                    'date' => $date,
                    'limit_date' => $limit_date,
        ));
    }

问题是它不会重定向到索引页面只是刷新页面 . 如果不正确的凭据,它会显示“错误”,否则如果正确的凭据,它只是刷新页面而不会重定向 . 我用它显示的成功消息替换重定向 . 我有相同的代码localy和登录与重定向是好的,但在谷歌应用程序引擎(我的项目在线)不重定向 .

1 回答

  • 1

    您使用的示例实际上不会重定向用户有两个原因 .

    • 使用 Redirect::route() 除了传递的参数是路由的名称,例如一个如此定义的路由

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

    要在此处重定向,您将使用 Redirect::route('home') .

    • 您实际上没有返回重定向 . 必须使用 return 关键字返回路径的任何响应,无论是在控制器方法还是闭包内 .

    所以要纠正你的代码,它是这样的:

    public function doLogin() {
        $credentials = [
            'email' => Input::get('email'), 
            'password' => Input::get('password')
        ];
    
        if (Auth::attempt($credentials)) {
            return Redirect::to('/');
        } else {
            dd('error');
        }
    }
    

    我将凭据移动到一个数组,因为它看起来更整洁,这使得在此网站上显示时更容易阅读,因此您不必这样做,但它可能会使您更容易 .

相关问题