Laravel 4.2 路由参数 %7b 用户名 %7D 不工作
Larevel 4.2 routing parameter %7Busername%7D not working
我正在使用 laravel 做一个小项目,但由于某种原因路由无法正常工作,请为我指明正确的方向,在 post 代码下方。
路由器
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 意味着您必须在创建用户时进行检查,因此用户名不会与任何冲突您现有的 URLs.
我正在使用 laravel 做一个小项目,但由于某种原因路由无法正常工作,请为我指明正确的方向,在 post 代码下方。
路由器
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 意味着您必须在创建用户时进行检查,因此用户名不会与任何冲突您现有的 URLs.