我编辑了存储方法,现在的问题是,当我尝试登录时,它重定向到www.example.com/admin,但它显示的是一个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();
}}
the 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';
}我还安装了多读,这里是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,
),
);发布于 2015-06-15 22:33:54
在您的管理模板中,您将goto设置为sessions.store,它在该方法中命中SessionsController::store,您有一个调试函数dd(),它正在抛出该字符串。之所以调用它,是因为auth::attempt()按照您自己的代码返回false:
if($attempt) return Redirect::intended('/');所以这种行为完全是在做它应该做的事情。如果您成功登录,您将被重定向,否则它将通过dd()转储。
发布于 2015-06-16 02:57:52
你要做的就是过滤。您必须在app/filter.php上添加自定义错误消息。
如下所示
Route::filter('auth', function()
{
if (Auth::guest())
{
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
else
{
return Redirect::guest('/')->with('message', 'Please login first');
}
}
});以上代码,我重定向到/,并给第一条消息登录。
https://stackoverflow.com/questions/30853312
复制相似问题