Search code examples
phplaravel-4routesphp-5.5cartalyst-sentry

Larevel 4.2 routing parameter %7Busername%7D not working


Im doing small project using laravel, but the routing is not working for some reason, please point me to the right direction, below i post the code.

Router

Route::get('{username}', array('as' => 'profile', 'uses' => 'ProfileController@index'));

Controller

  public function index($username = null) {
        $username = Sentry::getUser()->username;

        return View::make('home.profile')
                 ->with('title', 'From controller')
->with('username', $username);

    }

Url http://social.app/%7Busername%7D

public function postLogin(){
        $credentials = Validator::make([
                    'email' => Input::get('email'),
                    'password' => Input::get('password')
                        ], [
                    'email' => 'required|email',
                    'password' => 'required|alpha_num|between:8,15',
        ]);
        if ($credentials->fails()) {
            return Redirect::route('login')
                            ->withInput()
                            ->with('title', 'Welcome to mySite | login')
                            ->withErrors($credentials->getMessageBag());
        } else {
            try {
                $credentials = array(
                    'email' => Input::has('email') ? Input::get('email') : null,
                    'password' => Input::has('password') ? Input::get('password') : null,
                );

                // Log the user in
                $user = Sentry::authenticate($credentials, Input::has('remember_me') and Input::get('remember_me') == 'checked');

                return View::make('home.profile');
            } catch (Cartalyst\Sentry\Users\LoginRequiredException $e) {
                return View::make('users.login')
                                ->with('title', 'wrong login')
                                ->with('message', 'Email filed is required');
            } catch (Cartalyst\Sentry\Users\PasswordRequiredException $e) {
                return View::make('users.login')
                                ->with('title', 'something went wrong')
                                ->with('message', 'Password filed is required');
            } catch (Cartalyst\Sentry\Users\WrongPasswordException $e) {
                return View::make('users.login')
                                ->with('title', 'wrong Password')
                                ->with('message', 'The password/login is incorrect.');
            } catch (Cartalyst\Sentry\Users\UserNotFoundException $e) {
                return View::make('users.login')
                                ->with('title', 'Not found')
                                ->with('message', 'Sorry we can't found the user.');
            }

        }
    }

Solution

  • I still can't see a redirect to profile in your login controller but I think you want something like this:

    // ...
    
    $user = Sentry::authenticate($credentials, Input::has('remember_me') and Input::get('remember_me') == 'checked');
    return Redirect::route('profile', $user->username);
    
    // ...
    

    Sidenote: Be aware that the username as root level URL means you have to check when creating the user, so the username doesn't collide with any of your existing URLs.