php Laravel 5.4 禁用注册路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42695917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:40:59  来源:igfitidea点击:

Laravel 5.4 Disable Register Route

phplaravellaravel-5.4

提问by Dev.Wol

I am trying to disable the register route on my application which is running in Laravel 5.4.

我正在尝试禁用在 Laravel 5.4 中运行的应用程序上的注册路由。

In my routes file, I have only the

在我的路由文件中,我只有

Auth::routes();

Is there any way to disable the register routes?

有没有办法禁用注册路由?

回答by dparoli

The code:

code

Auth::routes();

its a shorcut for this collection of routes:

这是这一系列路线的捷径:

// Authentication Routes...
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');

// Registration Routes...
Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
Route::post('register', 'Auth\RegisterController@register');

// Password Reset Routes...
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');

So you can substitute the first with the list of routes and comment out any route you don't want in your application.

因此,您可以将第一个替换为路由列表,并注释掉您的应用程序中不需要的任何路由。

Edit for laravel version => 5.7

编辑 laravel version => 5.7

In newer versions you can add a parameter to the Auth::routes()function call to disable the register routes:

在较新的版本中,您可以向Auth::routes()函数调用添加一个参数以禁用注册路由:

Auth::routes(['register' => false]);

The email verification routes were added:

添加了电子邮件验证路线:

Route::get('email/verify', 'Auth\VerificationController@show')->name('verification.notice');
Route::get('email/verify/{id}', 'Auth\VerificationController@verify')->name('verification.verify');
Route::get('email/resend', 'Auth\VerificationController@resend')->name('verification.resend');

BTW you can also disable Password Resetand Email Verificationroutes:

顺便说一句,您还可以禁用Password ResetEmail Verification路由:

Auth::routes(['reset' => false, 'verify' => false]);

回答by Irwing Reza

Since Laravel 5.7, a new $optionsparameter is introduced to the Auth::routes()method; through which you can pass an array to control the generation of the requiredroutes for user-authentication (valid entries can be chosen from the 'register', 'reset', or 'verify'string literals).

从 Laravel 5.7 开始,$optionsAuth::routes()方法引入了一个新参数;通过它可以传递一个数组控制的生成所需的用于用户认证的路由(有效条目可以从被选择'register''reset''verify'字符串文字)。

Auth::routes(['register' => false]);

回答by Elvis Mugabi

You could try this.

你可以试试这个。

Route::match(['get', 'post'], 'register', function(){
    return redirect('/');
});

Add those routes just below the Auth::routes()to override the default registration routes. Any request to the /registerroute will redirect to the baseUrl.

在 下方添加这些路由Auth::routes()以覆盖默认注册路由。对/register路由的任何请求都将重定向到 baseUrl。

回答by Snaver

This is deceptively easy! You just need to override two methods in your app/Http/Controllers/Auth/RegisterController.phpClass. See below which will prevent the form from being displayed and most importantly block direct POST requests to your application for registrations..

这看似简单!你只需要在你的app/Http/Controllers/Auth/RegisterController.php类中覆盖两个方法。请参阅下文,这将阻止显示表单,最重要的是阻止对您的应用程序的直接 POST 请求进行注册。

/**
 * Show the application registration form.
 *
 * @return \Illuminate\Http\Response
 */
public function showRegistrationForm()
{
    return redirect('login');
}

/**
 * Handle a registration request for the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function register(Request $request)
{
    abort(404);
}

回答by Karthik M

In web.php, Replace

在 web.php 中,替换

Auth::routes();

Auth::routes();

With

Auth::routes(['register' => false]);

Auth::routes(['register' => false]);

So that you can remove register route from default auth route list. i tried in 5.7 and it worked fine.

这样您就可以从默认身份验证路由列表中删除注册路由。我在 5.7 中尝试过,效果很好。

回答by asmmahmud

Though the above solutions work but, I think changing the middlewareto 'auth'in the App\Http\Controllers\Auth\RegisterControllerwill be one of the easiest solutions. This will redirect all visitors to login page if they want to access any of the registration routes. Like this:

虽然上述解决方案的工作,但是,我想改变middleware'auth'App\Http\Controllers\Auth\RegisterController将是最简单的解决方案之一。如果他们想访问任何注册路线,这会将所有访问者重定向到登录页面。像这样:

namespace App\Http\Controllers\Auth;
class RegisterController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

回答by Evan

I guess you can do it like this, in your web.php file:

我想你可以这样做,在你的 web.php 文件中:

Route::redirect('register', 'login', 301);

回答by glou1986

I suppose you want to restrict access to some pages for guests and only the admin can register a guest. You can achieve it by adding your own middleware on kernel.php file like below:

我想您想限制访客访问某些页面,只有管理员才能注册访客。您可以通过在 kernel.php 文件上添加您自己的中间件来实现它,如下所示:

protected $routeMiddleware = [
      'authenticated' => \App\Http\Middleware\AuthenticatedMiddleware::class
];

After creating the middleware you have to use it so you can go on web.php file where your routes are and add it to the route you want to restrict like below:

创建中间件后,您必须使用它,以便您可以访问您的路由所在的 web.php 文件,并将其添加到您要限制的路由中,如下所示:

Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register')->middleware('authenticated');
Route::post('register', 'Auth\RegisterController@register')->middleware('authenticated');

This way registration is restricted to guests but the admin can still access the page if he ever want to register some other admin!

这样注册仅限于访客,但如果管理员想注册其他管理员,他仍然可以访问该页面!

Don't forget to replace the Auth::routes();with the detailed list as below:

不要忘记将 替换Auth::routes();为详细列表,如下所示:

// Authentication Routes...
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');

// Registration Routes...
Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register')->middleware('authenticated');
Route::post('register', 'Auth\RegisterController@register')->middleware('authenticated');

// Password Reset Routes...
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');

回答by Abdelsalam Shahlol

On my Laravel 5.6project this method didn't work:

在我的 Laravel 5.6项目中,此方法不起作用:

Auth::routes(['register' => false]);

So I had to use the following method:

所以我不得不使用以下方法:

Route::match(['get', 'post'], 'register', function () {
    return abort(403, 'Forbidden');
})->name('register');

回答by sachinsuthariya

Add this two method to app\Http\Controllers\Auth\RegisterController.php

将这两个方法添加到 app\Http\Controllers\Auth\RegisterController.php

public function showRegistrationForm(){
    return redirect('login');
}

public function register(){

}