Laravel 5.2验证注册页面受阻 [英] Laravel 5.2 Auth Registration page blocked

查看:150
本文介绍了Laravel 5.2验证注册页面受阻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在与laravel web应用程序。我使用的是内置的laravel认证系统,并添加自己的排名。然而,当你登录后,你不能达到注册页面。这里的关键是,我想只有管理员才能够创建新用户。

I'm currently making a webapp with laravel. I'm using the build-in laravel authentication system, and added ranks myself. However, when you are logged in, you can't reach the registration page. THe thing here is, I want only admins to be able to create new users.

我的路线:

Route::group(['middleware' => ['web']], function () {
    Route::get('login', ['as' => 'auth.login', 'uses' => 'Auth\AuthController@showLoginForm']);
    Route::post('login', ['as' => 'auth.login', 'uses' => 'Auth\AuthController@login']);
    Route::get('logout', ['as' => 'auth.logout', 'uses' => 'Auth\AuthController@logout']);

// Password Reset Routes...
    Route::get('password/reset/{token?}', ['as' => 'auth.password.reset', 'uses' => 'Auth\PasswordController@showResetForm']);
    Route::post('password/email', ['as' => 'auth.password.email', 'uses' => 'Auth\PasswordController@sendResetLinkEmail']);
    Route::post('password/reset', ['as' => 'auth.password.reset', 'uses' => 'Auth\PasswordController@reset']);

    Route::get('/home', 'HomeController@index');
});

Route::group(['middleware' => ['CheckAdmin']], function () {
    // Registration Routes...
    Route::get('/register', 'Auth\AuthController@showRegistrationForm');
    Route::post('/register', 'Auth\AuthController@register');
});

我AuthController

My AuthController

<?php

namespace App\Http\Controllers\Auth;

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

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;

    protected $redirectTo = '/admin';

    /**
     * Create a new authentication controller instance.
     *
     * @param  \Illuminate\Contracts\Auth\Guard  $auth
     * @param  \Illuminate\Contracts\Auth\Registrar  $registrar
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }

    public function getRegister()
    {
        return redirect('/');
    }

    public function postRegister()
    {
        return redirect('/');
    }

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

    /**
     * Returns either ADMIN or USER based on user's rank
     * Returns false if user is not logged in
     */
    public static function getType()
    {
        if (Auth::check()) {
            return Auth::user()->type;
        }
        else {
            return false;
        }
    }
}

我试过在网上找到很多教程,但是对于我的那些工作没有一个。有没有人有我一个解决方案吗?在此先感谢!

I've tried many tutorials found online, but neither one of those work for me. Does anyone have a solution for me? Thanks in advance!

推荐答案

试试这几件事情加:

与建立管理中间件:

public function handle($request, Closure $next)
    {
    if ( Auth::check() && Auth::user()->isAdmin() )
    {
        return $next($request);
    }
    return redirect('/home');
}

然后在$ routeMiddleware这行添加到您的Kernel.php:

Then add this line to your Kernel.php under $routeMiddleware:

    'admin' => \App\Http\Middleware\Admin::class,

把管理员航线:

Route::group(['middleware' => ['auth', 'admin']], function () {
   //here 
});

这走了进去中间件=>网络组

This goes inside middleware =>web group

在这里其它路线:

Route::group(['middleware' => 'web'], function () {
   //here
});

这添加到你拥有的每控制器:

Add this to every controller you have:

public function __construct() { $this->middleware('auth'); } 

这篇关于Laravel 5.2验证注册页面受阻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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