Laravel 5.2登录不工作 [英] Laravel 5.2 Login not working

查看:278
本文介绍了Laravel 5.2登录不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的Laravel 5.2的安装。我已经运行下面的命令来安装默认Laravel认证;

  PHP工匠化妆:AUTH

该登记表的作品,但登录只是重定向家里没有登录。并显示没有错误的时候,当我输入了错误的凭据。

这是我的路线文件:

 路线::得到('/','BaseController @指数');路线::得到('/导师,TutorsController @指数');
路线::得到('/教师/ myrequest','TutorsController @ RequestTutor');
路线::得到('/教师/ {ID}','TutorsController @秀) - 化合物其中(['身份证'=>'[0-9] +']);
路线::得到('/教师/ {GUID}','TutorsController @ TutorByGUID') - 化合物其中(['身份证'=>'[A-ZA-Z] + [0-9] +']) ;
/ **********认证***************************航线******* /
路线::组(['中间件'=>'网络'],函数(){
    路线:: AUTH();
});

这是从code中的 AuthController

 类AuthController扩展控制器
{
使用AuthenticatesAndRegistersUsers,ThrottlesLogins;
保护$ redirectTo ='/';公共职能__construct()
{
   $这个 - >中间件($这个 - > guestMiddleware(),['除了'=>'注销']);
}/ **
 *获取一个验证传入注册请求。
 *
 * @参数数组$数据
 * @返回\\照亮\\合同\\确认\\验证
 * /
保护功能验证(数组$数据)
{
    返回验证::彩妆($数据[
        '名字'=> 需要|最高:255',
        '姓氏'=> 需要|最高:255',
        电子邮件=> 需要|电子邮件|最高:255 |独特:用户,
        '密码'=> 需要|证实|分:6',
    ]);
}/ **
 *一个有效的注册后创建一个新的用户实例。
 *
 * @参数数组$数据
 * @返回用户
 * /
保护函数来创建(数组$数据)
{
    返回用户::创建([
        '名字'=> $数据['名字'],
        '姓氏'=> $数据['姓氏'],
        电子邮件=> $数据[电子邮件]
        '密码'=> bcrypt($数据['密码'])
    ]);
}
}

这是 BaseController 包含Home方法;

 < PHP空间应用\\ HTTP \\控制器;
使用应用程序\\ SERVICES \\ InterfaceService;
使用应用程序\\库\\ TutorRepository;
使用应用程序\\ HTTP \\请求;
使用照亮\\ HTTP \\请求;类BaseController扩展控制器{    保护$ TutorRepository;
    保护$ InterfaceService;
    公共职能__construct(InterfaceService $ InterfaceService,TutorRepository $ TutorRepository)
    {
        // $这个 - >中间件('客人');
        $这个 - > InterfaceService = $ InterfaceService;
        $这个 - > TutorRepository = $ TutorRepository;
    }    公共功能指数()
    {
        $导师= $这个 - > TutorRepository->所有的();
        $ page_info = \\配置:: GET('constants.App.Pages.home');
        $这个 - > InterfaceService-> SetPageInfo($ page_info);
        返回视图('家',['TopTutors'=> $导师]);
    }?}>

这是从登录查看code。

 <形式的作用=形式的方法=POSTACTION ={{URL('/登录')}}ID =login_form>
    {! csrf_field()!}
    < D​​IV CLASS =mj_login_form>
        < D​​IV CLASS =表单组>
            <输入类型=文本占位符=电子邮件ID =电子邮件NAME =电子邮件级=表格控VALUE ={{老(电子邮件)}}>
            @if($错误 - >拥有(电子邮件))
                <跨度类=帮助块><强> {{$错误 - >首先(电子邮件)}}< / STRONG>< / SPAN>
            @万一
        < / DIV>
        < D​​IV CLASS =表单组>
            <输入类型=密码占位符=您的密码ID =密码级=表格控NAME =密码>
            @if($错误 - >拥有('密码'))
                <跨度类=帮助块><强> {{$错误 - >首先('密码')}}< / STRONG>< / SPAN>
            @万一
        < / DIV>
        < D​​IV CLASS =行>
            < D​​IV CLASS =COL-LG-6 COL-MD-6 COL-SM-12 COL-XS-12 mj_toppadder20>
                < D​​IV CLASS =表单群拉左>
                    < D​​IV CLASS =mj_checkbox>
                        <输入类型=复选框值=1ID =CHECK2NAME =记住>
                        <标签=CHECK2>< /标签>
                    < / DIV>
                    <跨度>记得我< / SPAN>
                < / DIV>
            < / DIV>
            < D​​IV CLASS =COL-LG-6 COL-MD-6 COL-SM-12 COL-XS-12 mj_toppadder20>
                < D​​IV CLASS =表单组右拉式>
                    <跨度>< A HREF ={{URL('/密码/ RESET')}}>忘记密码< / A>< / SPAN>
                < / DIV>
            < / DIV>
        < / DIV>
    < / DIV>
    < D​​IV CLASS =mj_pricing_footer>
        < A HREF =#的onclick =&GT$('#login_form)提交();现在登录< / A>
    < / DIV>
< /表及GT;


解决方案

这可能是因为你的/路由不是网​​络下中间件组,你不能使用你想被认证的路由 AUTH 中间件

这意味着,访问该路由时,会话不可用,而 AUTH 中间件不火,所以认证不起作用。

尽量把所有要在其中的网​​站中间件组下使用会话路由,并且要在其中认证使用<$ C $路由C> AUTH 中间件:

 路线::组(['中间件'=&GT;'网络'],函数(){    / *这些航线使用'权威性'中间件,所以只有通过身份验证的用户将获得* /
    路线::组(['中间件'=&GT;'权威性'],函数(){
        路线::得到('/','BaseController @指数');
    });    路线:: AUTH();
});

上Laravel版本A注:

请记住,如果你使用Laravel版本> = 5.2.27 网​​站使用中间件默认情况下,在 routes.php文件中定义的每个路径上,你可以看到在应用程序/供应商/ RouteServiceProvider.php

 保护功能mapWebRoutes(路由器$路由器)
{
    $路由器 - 基团([
        '空间'=&GT; $这个 - &GT;命名空间,'中间件'=&GT; 网络
    ]函数($路由器){
        需要APP_PATH('HTTP / routes.php文件');
    });
}

在这种情况下,你可以删除语句使用网​​站中间件组,但你只需要使用 AUTH 中间件的认证路线

I have created a new Laravel 5.2 installation. And I have run the following command to install the default Laravel authentication;

php artisan make:auth

The registration form works but the login just redirects home without logging in. And displays no errors when when I enter the wrong credentials.

This is my routes file:

Route::get('/', 'BaseController@index');

Route::get('/tutors', 'TutorsController@Index');
Route::get('/tutors/myrequest', 'TutorsController@RequestTutor');
Route::get('/tutors/{id}', 'TutorsController@show')->where(['id' => '[0-9]+']);
Route::get('/tutors/{GUID}', 'TutorsController@TutorByGUID')->where(['id' => '[A-Za-z]+[0-9]+']);


/********************Authentication routes**********************************/
Route::group(['middleware' => ['web']], function () {
    Route::auth();
});

This is code from the AuthController:

class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;


protected $redirectTo = '/';

public function __construct()
{
   $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    return Validator::make($data, [
        'firstname' => 'required|max:255',
        'lastname' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return User
 */
protected function create(array $data)
{
    return User::create([
        'firstname' => $data['firstname'],
        'lastname' => $data['lastname'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}
}

This is the BaseController which contains the home method;

<?php namespace App\Http\Controllers;
use App\Services\InterfaceService;
use App\Repositories\TutorRepository;
use App\Http\Requests;
use Illuminate\Http\Request;

class BaseController extends Controller {

    protected $TutorRepository;


    protected $InterfaceService;
    public function __construct(InterfaceService $InterfaceService, TutorRepository $TutorRepository)
    {
        //$this->middleware('guest');
        $this->InterfaceService = $InterfaceService;
        $this->TutorRepository = $TutorRepository;
    }

    public function index()
    {
        $tutors = $this->TutorRepository->all();
        $page_info = \Config::get('constants.App.Pages.home');
        $this->InterfaceService->SetPageInfo($page_info);
        return view('home', ['TopTutors'=>$tutors]);
    }

} ?>

This is code from the login view.

<form role="form" method="POST" action="{{ url('/login') }}" id="login_form">
    {!! csrf_field() !!}
    <div class="mj_login_form">
        <div class="form-group">
            <input type="text" placeholder="Email" id="email" name="email" class="form-control" value="{{ old('email') }}">
            @if ($errors->has('email'))
                <span class="help-block"><strong>{{ $errors->first('email') }}</strong></span>
            @endif
        </div>
        <div class="form-group">
            <input type="password" placeholder="Your Password" id="password" class="form-control" name="password">
            @if ($errors->has('password'))
                <span class="help-block"><strong>{{ $errors->first('password') }}</strong></span>
            @endif
        </div>
        <div class="row">
            <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 mj_toppadder20">
                <div class="form-group  pull-left">
                    <div class="mj_checkbox">
                        <input type="checkbox" value="1" id="check2" name="remember">
                        <label for="check2"></label>
                    </div>
                    <span> remember me</span>
                </div>
            </div>
            <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 mj_toppadder20">
                <div class="form-group pull-right">
                    <span><a href="{{ url('/password/reset') }}">forget password ?</a></span>
                </div>
            </div>
        </div>
    </div>
    <div class="mj_pricing_footer">
        <a href="#" onclick="$('#login_form').submit()">login Now</a>
    </div>
</form>

解决方案

That happens probably because your '/' route is not under the web middleware group and you're not using the auth middleware for the routes you want to be authenticated

This means that when accessing that route, session is not available, and the auth middleware doesn't fire, so authentication doesn't work.

Try to put all the routes in which you want to use session under the web middleware group, and for the routes in which you want authentication use the auth middleware:

Route::group( ['middleware' => ['web'] ], function () {     

    /* these routes use 'auth' middleware, so only an authenticated user will access*/
    Route::group( ['middleware' => 'auth' ], function () {
        Route::get('/', 'BaseController@index');
    });

    Route::auth();
});

A Note on Laravel version:

Keep in mind that if you're using Laravel version >= 5.2.27 the middleware web is used by default on every route defined in routes.php as you can see in app/Providers/RouteServiceProvider.php:

protected function mapWebRoutes(Router $router)
{
    $router->group([
        'namespace' => $this->namespace, 'middleware' => 'web'
    ], function ($router) {
        require app_path('Http/routes.php');
    });
}

In that case you can remove you statement for using the web middleware group, but you'll only need to use auth middleware for the authenticated routes

这篇关于Laravel 5.2登录不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆