Laravel Make Auth,隐藏身份验证背后的注册?

时间:2016-07-25 20:35:53

标签: laravel laravel-5 laravel-5.2 laravel-routing artisan

所以我最近决定尝试命令

php artisan make:auth

而不是自己创建所有auth,它似​​乎工作,我继续我的应用程序。但是现在我正处于我的应用程序中,我不想要一个面向公众的注册页面(我希望这只是管理员才能添加新用户)。但是,该命令不会插入单独的路由或函数,而是内置于laravel中的内容。通常我只能使用

$this->middleware('auth');

处理寄存器的控制器内部,但由于这是与登录相同的控制器导致问题....有没有人知道如何保持由php artisan创建的注册页面make:auth在登录墙后面所以只有管理员可以注册新用户?

2 个答案:

答案 0 :(得分:1)

首先:取消注册用户的当前路由。 (有几种方法可以这样做)ex:乘坐Route :: auth();来自route.php并且仅应用您想要应用的路线。 router.php中定义的auth()如下:

/**
 * Register the typical authentication routes for an application.
 *
 * @return void
 */
public function auth()
{
    // Authentication Routes...
    $this->get('login', 'Auth\AuthController@showLoginForm');
    $this->post('login', 'Auth\AuthController@login');
    $this->get('logout', 'Auth\AuthController@logout');

    // Registration Routes...
    $this->get('register', 'Auth\AuthController@showRegistrationForm');
    $this->post('register', 'Auth\AuthController@register');

    // Password Reset Routes...
    $this->get('password/reset/{token?}', 'Auth\PasswordController@showResetForm');
    $this->post('password/email', 'Auth\PasswordController@sendResetLinkEmail');
    $this->post('password/reset', 'Auth\PasswordController@reset');
}

第二:使用中间件Auth应用ex:

创建自己的路由
Route::get('users/create', [
    'middleware' => 'auth', 
    'as' => 'users.create', 
    'uses' => 'Auth\AuthController@create'
]);

您可以以不同的方式应用中间件,请参阅此链接以获取更多信息: https://laravel.com/docs/5.2/routing#route-group-middleware

答案 1 :(得分:0)

基于this,您似乎应该能够创建自己的使用AuthenticatesAndRegistersUsers特征的Auth控制器,然后覆盖getRegister and postRegister方法。

类似的东西:

<?php

namespace App\Http\Controllers\Auth;

use Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{
    use AuthenticatesAndRegistersUsers;

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application registration form.
     * @return Response
     */
    public function getRegister()
    {
        // show register view
    }

    /**
     * Handle a registration request for the application.
     * @return Response
     */
    public function postRegister(Request $request)
    {
        // register logic
    }

}