首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >管理员登录laravel 4

管理员登录laravel 4
EN

Stack Overflow用户
提问于 2015-06-15 19:20:20
回答 2查看 282关注 0票数 0

我编辑了存储方法,现在的问题是,当我尝试登录时,它重定向到www.example.com/admin,但它显示的是一个NotFoundHttpException.

routes.php文件

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
{{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模型

代码语言:javascript
复制
    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文件

代码语言:javascript
复制
    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,

        ),

    );
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-15 22:33:54

在您的管理模板中,您将goto设置为sessions.store,它在该方法中命中SessionsController::store,您有一个调试函数dd(),它正在抛出该字符串。之所以调用它,是因为auth::attempt()按照您自己的代码返回false

代码语言:javascript
复制
if($attempt) return Redirect::intended('/');

所以这种行为完全是在做它应该做的事情。如果您成功登录,您将被重定向,否则它将通过dd()转储。

票数 0
EN

Stack Overflow用户

发布于 2015-06-16 02:57:52

你要做的就是过滤。您必须在app/filter.php上添加自定义错误消息。

如下所示

代码语言:javascript
复制
Route::filter('auth', function()
{
    if (Auth::guest())
    {
        if (Request::ajax())
        {
            return Response::make('Unauthorized', 401);
        }
        else
        {
            return Redirect::guest('/')->with('message', 'Please login first');
        }
    }
});

以上代码,我重定向到/,并给第一条消息登录。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30853312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档