2

My application doesn't want to go in my GameController.php getIndex() function. It does go in the group function , I tested this with dd('test') vardump. It goes in it until it bumps into the 'game' route then it just doesn't want to go in this route.

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);

            }


        }

    }

}

The goal is to go to the 'game' page when my user is logged in, this is the case because the Laravel session cookie is made (I checked) and I can access the route manually by typing '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' and strangely this page is always empty.

Jim Peeters
  • 2,573
  • 9
  • 31
  • 53
  • 1
    You don't need the slash in your route definition. Just `Route::get('game', ...` should work. – Stuart Wagner Jun 28 '15 at 11:02
  • Is the user not redirected to the `game` route after logging in? Or is the route not working at all? – Stuart Wagner Jun 28 '15 at 11:09
  • well , I created code that when it is a username that doesn't exist it creates a new user and after the creation it automatically logs this user in , but it seems to always go back to the '/' route after this. but it does go in the group function so that is what I find weird. I will add my AuthenticationController.php to my question to make it clear. – Jim Peeters Jun 28 '15 at 11:13
  • 1
    Also, this looks like Laravel 4. Could you label or tag it as such if so? With Laravel 5 out now, it's important to know what you're asking about. – Stuart Wagner Jun 28 '15 at 11:15
  • I did this now , thanks for noticing – Jim Peeters Jun 28 '15 at 11:16
  • Ok, good. I see that you already have a `intended('game')` call to redirect the user. However, this takes a full URL, not a named route, so you'll need to change it to `intended('/user/game')` to account for your route group prefix. – Stuart Wagner Jun 28 '15 at 11:18
  • oh I removed the prefix , I have edited this in my question , because I am making changes when people notice something but I have forgot to change them in my question as well , again thanks :) – Jim Peeters Jun 28 '15 at 11:20
  • So does the redirect work now? If not, try `intended('/game')` in your auth controller. – Stuart Wagner Jun 28 '15 at 11:23
  • I tried return 'Redirect::intended('/game'); ' but this is also not working , it is returning me to 'http://localhost:8000/' – Jim Peeters Jun 28 '15 at 18:09
  • this is also an empty page , untill i refresh it – Jim Peeters Jun 28 '15 at 18:23

1 Answers1

0

I fixed it , it seems like the postLogin() method had a postlogin() method inside it. This was causing the error. I changed these lines in the AuthenticationController and it worked.

replaced these lines :

$this->postLogin();

with these :

if (Auth::attempt(array('username' => $inputUsername, 'password' => $inputPassword)))
        {
            return Redirect::to('game');    
        }
Jim Peeters
  • 2,573
  • 9
  • 31
  • 53