如何访问Laravel的这条路线?

我的应用程序不想进入我的GameController.php getIndex() 函数 . 它确实进入了组功能,我用 dd('test') vardump进行了测试 . 它进入它直到它碰到'game'路线然后它只是不想进入这条路线 .

routes.php:

Route::group(array('before' => 'auth'), function()
{
    //dd('test') , this worked
    Route::get('/game', array('as' => 'game','uses' =>'GameController@getIndex'));
});

GameController.php:

class GameController extends BaseController {

    public function getIndex()
    {
        //dd('test') , he didnt do this dump
        $items = DB::table('tblItems_Users')->where('FK_user_id', '=', Auth::user()->PK_user_id)
            ->join('tblItems', 'FK_item_id', '=', 'PK_item_id')->where('is_checked', '=', 0)
            ->get();
        $item = $items[array_rand($items)];
        return View::make('game')->with('item', $item);
     }

}

AuthenticationController.php:

class AuthenticationController extends Controller
{

    //login methode voor de http-get login request --> loginformulier weergeven
    public function getLogin()
    {
        return View::make('login');
    }

    //login methode voor de http-post login request, wanneer er op de submitknop 'inloggen' gedrukt w
    public function postLogin()
    {
        //validation rules toevoegen, zodat er geen leeg inlogformulier gesubmit kan worden
        $validationRules = array(   'username' => 'required',   
                                    'password' => 'required'); //email is verplicht, en moet volgens emailformaat //paswd is verplicht, en minimum 8karakters lang

        //The first argument passed to the make method is the data under validation. The second argument is the validation rules that should be applied to the data.
        $validator = Validator::make(Input::all(), $validationRules);

        if ($validator->fails()) 
        {
            dd('validator fails');

            return Redirect::route('login')->withErrors($validator)->withInput(Input::only('username')); 

        }

        $inputUsername = Input::get('username');
        $inputPassword = Input::get('password');

        if (Auth::attempt(array('username' => $inputUsername, 'password' => $inputPassword)))
        {
            //dd('test')  => this is working
            return Redirect::intended('/game');
        }
        else
        {
            $users = DB::table('tblUsers')->lists('username');

            if(in_array($inputUsername, $users))
            {
                return Redirect::route('login')->withErrors(array('Oops! The password you entered is incorrect.')); 
            }
            else
            {
                $this->storeUser();
                $this->postLogin();
            }
        }
    }

    public function getLogout()
    {
        Auth::logout();
        return Redirect::route('getLogin')->with('logoutSuccessMessage', "You're succesfully signed out! See you next time!"); 
    }



    public function storeUser()
    {


        $rules = array( 'username'      => 'required|max:100',
                        'password'      => 'required|max:60'); 

        $validator = Validator::make(Input::all(), $rules);

        if ($validator->fails()) 
        {
            return Redirect::route('login')->withErrors($validator)->withInput(Input::only('username')); 
        }
        else
        {


            $inputPassword = Input::get('password');
            $hashedPassword = Hash::make($inputPassword);

            $user = new User;


            $user->username                 = Input::get('username');
            $user->password                 = $hashedPassword;

            $itemids = DB::table('tblItems')->lists('PK_item_id');

            $user->save();


            foreach($itemids as $itemid)
            {

                $user->items()->attach($itemid);

            }


        }

    }

}

我的目标是在我的用户登录时转到'game'页面,这是因为Laravel会话cookie已经完成(我已经检查过),我可以通过输入'http://localhost:8000/user/game '. I also am making a new user and then after it is made logging him in automatically in the AuthenticationController.php file. (maybe it is also needed to know that users and items are in a many to many relationship). I am always getting redirected to ' http://localhost:8000'来手动访问路线,奇怪的是这个页面是总是空的 .

回答(1)

2 years ago

我修复了它,似乎postLogin()方法里面有一个postlogin()方法 . 这导致了错误 . 我在AuthenticationController中更改了这些行并且它有效 .

取代这些线:

$this->postLogin();

用这些 :

if (Auth::attempt(array('username' => $inputUsername, 'password' => $inputPassword)))
        {
            return Redirect::to('game');    
        }