简体   繁体   中英

Laravel 5.1 Basic Authentication

I am newbie to laravel, I have several doubts here to ask. It's like when I do basic login and registeration using Auth Class which is by default provided by Laravel 5.1, it gives me 404 Not found error.

Here is my directory structure:

resources/views/auth/register.blade.php
resources/views/auth/login.blade.php

I am following laravel 5.1 official doc, to get this. When I press for submit button in register form it throws me 404 not found error.

register.blade.php

<!-- resources/views/auth/register.blade.php -->

<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>

I have my auth class as a :

app/Http/Controllers/Auth/AuthController.php
app/Http/Controllers/Auth/PasswordController.php

My basic routes:

// Authentication routes...
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');


Route::controllers([
   'password' => 'Auth\PasswordController',
]);

Still it throws me 404 not found error. Any Solution What I am missing? Also the routes shows AuthController@getRegister , do I have to manually create the function of getRegister , as I couldn't find any.? I am new to laravel

Also my AuthController.php looks like

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Requests;
use Illuminate\Http\Request;
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;
    protected $redirectPath = '/dashboard';
    protected $loginPath = '/login';
    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * 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',
        ]);
    }

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

I am not sure, but I guess the problem is coming from your action attribute in form. Never use plain url in form action in laravel.
Try to use

<form method="POST" action="{{ url('/auth/login') }}">


You can create also your forms in this way:(You have to add the dependency in your composer.json, search in google about laravel forms)

{!! Form::open(array('url' => 'http://other.domain.com')) !!}
  <input type="text" name="test" value="something" />
  <button type="submit">Submit</button>
{!! Form::close() !!}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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