简体   繁体   English

PHP-Laravel 5.2身份验证不起作用

[英]php - Laravel 5.2 Auth not Working

I want to make a authentication user login and register in laravel. 我想进行身份验证用户登录并在laravel中注册。 But when I submit to save the register info..It's showing 但是当我提交以保存注册信息时。显示

Object not found! 找不到对象!

The requested URL was not found on this server. 在此服务器上找不到请求的URL。 The link on the referring page seems to be wrong or outdated. 推荐页面上的链接似乎有误或已过时。 Please inform the author of that page about the error. 请将该错误告知该页面的作者。 If you think this is a server error, please contact the webmaster. 如果您认为这是服务器错误,请与网站管理员联系。

Here is my authController: 这是我的authController:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */


    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['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, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }


    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

    protected function getLogin() {
        return View('auth.login');
    }

    protected function postLogin(LoginRequest $request) {
        if ($this->auth->attempt($request->only('email', 'password'))) {
                return redirect()->route('/');
          //  return view('course.index');
        }

        return redirect('auth/login')->withErrors([
            'email' => 'The email or the password is invalid. Please try again.',
        ]);
    }

    /* Register get post methods */
    protected function getRegister() {
        return View('auth.register');
    }

    protected function postRegister(RegisterRequest $request) {
        $this->user->name = $request->name;
        $this->user->email = $request->email;
        $this->user->password = bcrypt($request->password);
        $this->user->save();
        return redirect('auth.login');
    }


  protected function getLogout()
    {
        $this->auth->logout();
        return redirect('auth.login'); 
    } 
    protected $redirectTo = '/course';
    protected $loginPath = '/auth/login';

}

Here is my login.blade.php file: 这是我的login.blade.php文件:

    <form method="POST" action="/auth/login">
        {!! csrf_field() !!}

        <div>
            Email
            <input type="email" name="email" value="{{ old('email') }}">
        </div>

        <div>
            Password
            <input type="password" name="password" id="password">
        </div>

        <div>
            <input type="checkbox" name="remember"> Remember Me
        </div>

        <div>
            <button type="submit">Login</button>
        </div>
    </form>

Here is my register.blade.php file

<form method="POST" action="/auth/register">
    {!! csrf_field() !!}

    <div>
        Name
        <input type="text" name="name" value="{{ old('name') }}">
    </div>

    <div>
        Email
        <input type="email" name="email" value="{{ old('email') }}">
    </div>

    <div>
        Password
        <input type="password" name="password">
    </div>

    <div>
        Confirm Password
        <input type="password" name="password_confirmation">
    </div>

    <div>
        <button type="submit">Register</button>
    </div>
</form>

And Here is the routes.php 这是routes.php

<?php

/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('/all_user',function(){
    return view('all_user');
});

Route::get('all_user/{id}/{name}',function($id,$name){      // Here we pass the peremeter in url all_user
    return 'User '.$id." ".$name;                         // with the parameter id and name
});

Route::get('home','basicController@index'); // Here Home is the URL and it 
                                            //execute the basiccontroller index page
Route::get('about','basicController@about');

Route::resource('course','courseController');

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/


Route::group(['middleware' => ['web']], function () {
   Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');

// Registration routes...
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');
});

In your view 在您看来

For login : 登录:

{{ !! Form::open(array('route'=>route('auth.login'))) !!}}

For Register : 报名:

{{ !! Form::open(array('route'=>route('auth.register'))) !! }}

In you Routes 在你的路线

For login 登录

Route::post('auth/login', array('as'=>'auth.login','uses'=>'Auth\AuthController@postLogin'));

For Register 报名

Route::post('auth/register', array('as'=>'auth.register','uses'=>'Auth\AuthController@postRegister'));

And don't forgot to close your form as : 并且不要忘记关闭您的表单,因为:

{{ !! Form::close() !! }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM