我正在使用laravel做小项目,但由于某种原因路由不起作用,请指出我正确的方向,下面我发布代码。
路由器
Route::get('{username}', array('as' => 'profile', 'uses' => 'ProfileController@index'));
控制器
public function index($username = null) {
$username = Sentry::getUser()->username;
return View::make('home.profile')
->with('title', 'From controller')
->with('username', $username);
}
网址 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.');
}
}
}
我仍然无法在您的登录控制器中看到重定向到profile
,但我认为您想要这样的东西:
// ...
$user = Sentry::authenticate($credentials, Input::has('remember_me') and Input::get('remember_me') == 'checked');
return Redirect::route('profile', $user->username);
// ...
旁注:请注意,用户名作为根级 URL 意味着您必须在创建用户时进行检查,以便用户名不会与任何现有 URL 冲突。