dongni1892 2015-06-15 19:20
浏览 40
已采纳

管理员登录laravel 4

I edited the store method , now the problem is that when i try to login it redirect to www.example.com/admin but it shows a NotFoundHttpException.

The routes.php file

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']]);

here is the controller 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()}}

and here is the model 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';



    }

I also installed ollieread multiauth and here is auth.php file

    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条回答 默认 最新

  • drebew5059 2015-06-15 22:33
    关注

    In your admin template you set the goto url as sessions.store which hits SessionsController::store in that method you have a debug function dd() which is throwing that string. It get's called because auth::attempt() returns false as by your own code:

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

    So the behavior is exactly doing what it should. If you are succesfully logged in, you are redirected otherwise it will dump through dd()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分