在laravel 5

时间:2015-07-07 10:39:17

标签: laravel-5

我无法在管理员方面登录laravel 5 我在mysql中的用户表数据是

username=admin   Primary key
password=admin

我的代码中是否有任何问题,如果那时请告诉我...

我的代码正在关注

Route.php

Route::group(array('prefix'=>'admin'),function(){

    Route::filter('pattern: admin/*', 'auth');
    Route::get('login', 'admin\AdminHomeController@showLogin');
    Route::post('check','admin\AdminHomeController@checkLogin');
    Route::get('adminlogout', 'admin\AdminHomeController@logout');

    Route::get('dashboard', 'admin\AdminHomeController@showDashboard');
});

Users.php //我的模块

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword;


    public static $auth_rules=[
        'username'=>'required',
        'password'=>'required'
    ];
    /**
     * The database table used by the model.
     *
     * @var string
     */
    public $timestamps=false;
    protected $table = 'users';
    public $primaryKey = 'username';


    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['username', 'password'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    public function getAuthPassword()
    {
        return $this->password;
    }

    /**
     * Automatically Hash the password when setting it
     * @param string $password The password
     */
    public function setPassword($password)
    {
        $this->password = Hash::make($password);
    }

}

AdminHomeController.php

<?php namespace App\Http\Controllers\admin;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Auth;

class AdminHomeController extends Controller {

    //

    public function showLogin()
    {
        return view('admin.login');
    }

    public function checkLogin(Request $request)
    {   
        $data=array(
            'username'=>$request->get('username'),
            'password'=>$request->get('password')
        );

        print_r($data);
        if(Auth::attempt($data))
        {
            echo "ok";die;
            return redirect::intended('admin/dashboard');
        }
        else
        {
            echo "not ok";die;
            return redirect('admin/login');
        }       
    }

    public function logout()
    {
        Auth::logout();
        return redirect('admin/login');
    }
    public function showDashboard()
    {
        return view('admin.dashboard');
    }
}

2 个答案:

答案 0 :(得分:1)

如果您在注册/添加管理员用户时使用以下功能设置密码,

变化

Hash::make($password)

bcrypt($password)

答案 1 :(得分:1)

我不知道是什么让您在(\)文件中使用反斜杠routes.php

在您的checkLogin方法中,您正尝试登录,我认为只要登录凭据正确,它就会在其信息中心中记录管理员。

您正在使用die;来停止执行脚本,因此没有重定向。

此外,您还没有提到尝试登录时遇到的错误是什么。

更改此代码:

/**
 * Automatically Hash the password when setting it
 *
 * @param string $password The password
 */
public function setPassword($password)
{
    $this->password = Hash::make($password);
}

到此:

/**
 * Automatically Hash the password when inserting
 *
 * @param string $password The password
 */
public function setPasswordAttribute($password)
{
    // You need to import the Hash Facade in
    // to hash your password.
    $this->password = Hash::make($password);

    // or use this if you do not want to import
    // the Hash Facade.
    $this->password = bcrypt($password);
}

每当您想要将属性设置为其对应/相应值时,您应该在属性前加上单词 set ,并在后面添加带有属性一词的相同属性。 (注意属性中的大写A)。因此,在您的代码中,它应该是setPasswordAttribute。然后,Laravel将保存您希望它保存在数据库表中的值。

但是为了让你走,我就是这样做的:

<强> routes.php文件

Route::get('/admin', 'UsersController@getAdminLogin');
Route::get('/admin/dashboard', 'UsersController@dashboard');
Route::post('/admin', 'UsersController@postAdminLogin');

<强> admin_login.blade.php

{!! Form::open(['url' => '/admin']) !!}
    <div class="form-group">
        {!! Form::label('email', 'Email Id:') !!}
        {!! Form::text('email', null, ['class' => 'form-control input-sm']) !!}
    </div>
    <div class="form-group">
        {!! Form::label('password', 'Password') !!}
        {!! Form::password('password', ['class' => 'form-control input-sm']) !!}
    </div>
    <div class="form-group">
        {!! Form::submit('Login', ['class' => 'btn btn-primary btn-block']) !!}
    </div>
{!! Form::close() !!}

<强> dashboard.blade.php

<h4 class="text-center">
    Welcome Admin, {{ Auth::user()->username }}
</h4>

<强> UsersController.php

/**
 * Display the admin login form if not logged in,
 * else redirect him/her to the admin dashboard.
 *
 */
public function getAdminLogin()
{
    if(Auth::check() && Auth::user()->role === 'admin')
    {
        return redirect('/admin/dashboard');
    }
    return view('admin_login');
}

/**
 * Process the login form submitted, check for the
 * admin credentials in the users table. If match found,
 * redirect him/her to the admin dashboard, else, display
 * the error message.
 *
 */
public function postAdminLogin(Request $request)
{
    $this->validate($request, [
        'email'    => 'required|email|exists:users,email,role,admin',
        'password' => 'required'
    ]);

    $credentials = $request->only( 'email', 'password' );

    if(Auth::attempt($credentials))
    {
        return redirect('/admin/dashboard');
    }
    else
    {
        // Your logic of invalid credentials.
        return 'Invalid Credentials';
    }
}

/**
 * Display the dashboard to the admin if logged in, else,
 * redirect him/her to the admin login form.
 *
 */
public function dashboard()
{
    if(Auth::check() && Auth::user()->role === 'admin')
    {
        return view('admin.dashboard');
    }
    return redirect('/admin');
}

访问this link以获取有关路线的更多信息。