laravel 4 中的错误
Error in laravel 4
当我尝试登录时,它会将我重定向到 www.example.com/admin/{username} 但它显示 NotFoundHttpException 错误。提前致谢!
routes.php
文件
Route::get('/admin', 'SessionsController@create');
Route::get('/logout', 'SessionsController@destroy');
Route::get('profile', function()
{
return "welcome! Your username is" . Auth::admin()->username;
});
Route::resource('sessions', 'SessionsController', ['only' => ['index', 'create', 'destroy', 'store']]);
这里是控制器SessionsController.php
<?php
class SessionsController extends \BaseController {
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
return View::make('admins');
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
$rules = array('username' => 'required', 'password' => 'required');
$validator = Validator::make(Input::all(), $rules);
if($validator -> passes()){
$credentials = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
if(Auth::admin($credentials,true)){
$username = Input::get('username');
return Redirect::to("admin/{$username}");
} else {
return Redirect::to('/admin')->withErrors('Username or password invalid');
}
} else {
return Redirect::to('/admin')->withErrors($validator->messages());
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
Auth::logout();
return Redirect::home();
}
}
admins.blade.php
{{Form::open(array('route' => 'sessions.store'))}}
<h1>ADMIN LOGIN </h1>
{{Form::label('username', 'Username')}}
{{Form::text('username')}}
{{Form::label('password', 'Password')}}
{{Form::password('password')}}
{{Form::submit('Login')}}
{{$errors->first()}}
{{Form::close()}}
这是模型 Admin.php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class Admin extends Eloquent implements UserInterface, RemindableInterface {
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'admins';
}
我也安装了ollieread multiauth
这是 auth.php
文件
return array(
'multi' => array(
'admin' => array(
'driver' => 'database',
'model' => 'Admin',
'table' => 'admins'
),
'user' => array(
'driver' => 'eloquent',
'model' => 'User',
'table' => 'users'
)
),
'reminder' => array(
'email' => 'emails.auth.reminder',
'table' => 'password_reminders',
'expire' => 60,
),
);
您尚未声明任何指向 /admin/{username}
的路由。这就是laravel.
找不到它的原因
这是一个例子:
在你的路线文件中
Route::get('/admin/{username}', 'SessionsController@getByUserName');
我强烈建议您使用命名路由。它们更易于管理,即使您的应用程序变得更大。
例如
在你的路线文件中
Route::get('/admin/{username}',array('as' => 'admin.username', 'uses'=>'SessionsController@getByUserName'));
然后你可以使用
调用指向某个控制器的url
URL::route('admin.username')
(内部视图)或
Redirect::route('admin.username')
(控制器内部)
您还可以使用数组传递变量。像这样
URL::route('admin.username',array($user->username))
更多关于路由的信息here
当我尝试登录时,它会将我重定向到 www.example.com/admin/{username} 但它显示 NotFoundHttpException 错误。提前致谢!
routes.php
文件
Route::get('/admin', 'SessionsController@create');
Route::get('/logout', 'SessionsController@destroy');
Route::get('profile', function()
{
return "welcome! Your username is" . Auth::admin()->username;
});
Route::resource('sessions', 'SessionsController', ['only' => ['index', 'create', 'destroy', 'store']]);
这里是控制器SessionsController.php
<?php
class SessionsController extends \BaseController {
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
return View::make('admins');
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
$rules = array('username' => 'required', 'password' => 'required');
$validator = Validator::make(Input::all(), $rules);
if($validator -> passes()){
$credentials = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
if(Auth::admin($credentials,true)){
$username = Input::get('username');
return Redirect::to("admin/{$username}");
} else {
return Redirect::to('/admin')->withErrors('Username or password invalid');
}
} else {
return Redirect::to('/admin')->withErrors($validator->messages());
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
Auth::logout();
return Redirect::home();
}
}
admins.blade.php
{{Form::open(array('route' => 'sessions.store'))}}
<h1>ADMIN LOGIN </h1>
{{Form::label('username', 'Username')}}
{{Form::text('username')}}
{{Form::label('password', 'Password')}}
{{Form::password('password')}}
{{Form::submit('Login')}}
{{$errors->first()}}
{{Form::close()}}
这是模型 Admin.php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class Admin extends Eloquent implements UserInterface, RemindableInterface {
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'admins';
}
我也安装了ollieread multiauth
这是 auth.php
文件
return array(
'multi' => array(
'admin' => array(
'driver' => 'database',
'model' => 'Admin',
'table' => 'admins'
),
'user' => array(
'driver' => 'eloquent',
'model' => 'User',
'table' => 'users'
)
),
'reminder' => array(
'email' => 'emails.auth.reminder',
'table' => 'password_reminders',
'expire' => 60,
),
);
您尚未声明任何指向 /admin/{username}
的路由。这就是laravel.
这是一个例子:
在你的路线文件中
Route::get('/admin/{username}', 'SessionsController@getByUserName');
我强烈建议您使用命名路由。它们更易于管理,即使您的应用程序变得更大。
例如
在你的路线文件中
Route::get('/admin/{username}',array('as' => 'admin.username', 'uses'=>'SessionsController@getByUserName'));
然后你可以使用
调用指向某个控制器的urlURL::route('admin.username')
(内部视图)或
Redirect::route('admin.username')
(控制器内部)
您还可以使用数组传递变量。像这样
URL::route('admin.username',array($user->username))
更多关于路由的信息here