【问题标题】:How to access this route in Laravel?如何在 Laravel 中访问这条路线?
【发布时间】:2015-09-14 22:23:40
【问题描述】:

我的应用程序不想进入我的 GameController.php getIndex() 函数。它确实在组功能中,我用dd('test') vardump 对此进行了测试。它进入它,直到它碰到“游戏”路线,然后它就不想进入这条路线。

routes.php:

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

游戏控制器.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);

            }


        }

    }

}

目标是在我的用户登录时转到“游戏”页面,这是因为制作了 Laravel 会话 cookie(我检查了),我可以通过键入“http://localhost:8000/user/game”手动访问路由.我也在创建一个新用户,然后在 AuthenticationController.php 文件中自动登录他。 (也许还需要知道用户和项目处于多对多关系)。我总是被重定向到“http://localhost:8000”,奇怪的是这个页面总是空的。

【问题讨论】:

  • 路由定义中不需要斜杠。只需Route::get('game', ... 就可以了。
  • 用户登录后没有重定向到game路由吗?还是这条路线根本不起作用?
  • 好吧,我创建了一个代码,当它是一个不存在的用户名时,它会创建一个新用户,并在创建后自动将该用户登录,但它似乎总是回到' /' 在此之后的路线。但它确实进入了组功能,所以我觉得很奇怪。我会将我的 AuthenticationController.php 添加到我的问题中以使其清楚。
  • 另外,这看起来像 Laravel 4。如果是这样,你能这样标记或标记它吗?随着 Laravel 5 的发布,重要的是要知道你在问什么。
  • 我现在这样做了,感谢您的关注

标签: php laravel laravel-4 routes


【解决方案1】:

我修好了,好像 postLogin() 方法里面有一个 postlogin() 方法。这是导致错误的原因。我在 AuthenticationController 中更改了这些行,它起作用了。

替换了这些行:

$this->postLogin();

这些:

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

【讨论】:

    猜你喜欢
    • 2017-07-23
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多