管理员登录laravel 4

时间:2015-06-15 19:20:20

标签: php laravel

我编辑了商店方法,现在的问题是,当我尝试登录时,会重定向到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();
}

}

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,

        ),

    );

2 个答案:

答案 0 :(得分:0)

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

if($attempt) return Redirect::intended('/');

所以这种行为正是应该做的。如果您已成功登录,则会被重定向,否则将通过dd()

进行转储

答案 1 :(得分:0)

你要做的就是过滤。您必须在Assesments = new Mongo.Collection('assesments'); Assesments.before.insert(function (userId, doc) { doc.createdAt = new Date(); }); Assesments.attachSchema(new SimpleSchema({ name: { type: String, label: 'First Name', autoform: { 'label-type': 'floating', placeholder: 'First Name' } }, email: { type: String, label: 'Email', autoform: { 'label-type': 'floating', placeholder: 'Email' } }, category: { type: String, label: 'Category', optional: true, autoform: { options: [ {value: 'General', label: 'General'}, {value: 'Reported', label: 'Reported'}, {value: 'Follow Up', label: 'Follow Up'} ], type: 'select-radio' } }, assesmentDate: { type: Date, label: 'Assesment Date', optional: true }, location: { type: String, label: 'Location', autoform: { 'label-type': 'floating', placeholder: 'Location' }, max: 200 }, createdBy: { type: String, autoValue: function() { return this.userId } } } )); if (Meteor.isServer) { Assesments.allow({ insert: function (userId, doc) { return true; }, update: function (userId, doc, fieldNames, modifier) { return true; }, remove: function (userId, doc) { return true; } }); }

上添加自定义错误消息

如下所示

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'); } } }); 并提供请先登录消息。