2017-03-09 106 views
16

我试图在运行Laravel 5.4的应用程序上禁用寄存器路由。Laravel 5.4禁用寄存器路由

在我的路由文件中,我只有Auth :: routes();

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

回答

28

代码:

Auth::routes(); 

其对这个集合路线的shorcut:

// Authentication Routes... 
    Route::get('login', 'Auth\[email protected]')->name('login'); 
    Route::post('login', 'Auth\[email protected]'); 
    Route::post('logout', 'Auth\[email protected]')->name('logout'); 

    // Registration Routes... 
    Route::get('register', 'Auth\[email protected]')->name('register'); 
    Route::post('register', 'Auth\[email protected]'); 

    // Password Reset Routes... 
    Route::get('password/reset', 'Auth\[email protected]')->name('password.request'); 
    Route::post('password/email', 'Auth\[email protected]')->name('password.email'); 
    Route::get('password/reset/{token}', 'Auth\[email protected]')->name('password.reset'); 
    Route::post('password/reset', 'Auth\[email protected]'); 

所以,你可以代替先用路由列表和注释掉你没有任何途径想要在你的应用程序中

-3

是的,有一种方法

Auth::routes(); 

远程从你的路由目录中的web.php这条路线。

该路由控制注册。

+0

downvoters这个答案并不完全错了,其只对了一半 –

+0

如果你这样做,你会禁用登录方法也一样,所以这个答案是错的! – user2519032

8

你可以试试这个。

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

添加只是Auth::routes()下面这些路线覆盖默认的注册途径。对/register路由的任何请求都将重定向到baseUrl。

+0

禁用'/ login'页面。 – Jason

+0

工作得很好。简单解决方案谢谢! – iaforek

1

我想你想限制访问某些页面的访客,只有管理员可以注册一个访客。您可以通过添加您自己的中间件上kernel.php文件象下面这样实现它:

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

创建中间件之后,你必须使用它,所以你可以去web.php文件在您的路线是将其添加到路线要限制象下面这样:

Route::get('register', 'Auth\[email protected]')->name('register')->middleware('authenticated'); 
Route::post('register', 'Auth\[email protected]')->middleware('authenticated'); 

这样的注册仅限于客人,但如果他想注册一些其它管理员管理员仍然能够访问该页面!

不要忘了如下的详细清单,以取代Auth::routes();

// Authentication Routes... 
Route::get('login', 'Auth\[email protected]')->name('login'); 
Route::post('login', 'Auth\[email protected]'); 
Route::post('logout', 'Auth\[email protected]')->name('logout'); 

// Registration Routes... 
Route::get('register', 'Auth\[email protected]')->name('register')->middleware('authenticated'); 
Route::post('register', 'Auth\[email protected]')->middleware('authenticated'); 

// Password Reset Routes... 
Route::get('password/reset', 'Auth\[email protected]')->name('password.request'); 
Route::post('password/email', 'Auth\[email protected]')->name('password.email'); 
Route::get('password/reset/{token}', 'Auth\[email protected]')->name('password.reset'); 
Route::post('password/reset', 'Auth\[email protected]'); 
0

更改路线:

供应商\ laravel \框架的\ src \照亮\路由\路由器。 PHP

public function auth() 
{ 
    // Authentication Routes... 
    $this->get('login', 'Auth\[email protected]')->name('login'); 
    $this->post('login', 'Auth\[email protected]'); 
    $this->post('logout', 'Auth\[email protected]')->name('logout'); 

    // Registration Routes... 
    //$this->get('register', 'Auth\[email protected]')->name('register'); 
    //$this->post('register', 'Auth\[email protected]'); 

    // Password Reset Routes... 
    //$this->get('password/reset', 'Auth\[email protected]')->name('password.request'); 
    //$this->post('password/email', 'Auth\[email protected]')->name('password.email'); 
    //$this->get('password/reset/{token}', 'Auth\[email protected]')->name('password.reset'); 
    //$this->post('password/reset', 'Auth\[email protected]'); 
} 
+0

对供应商文件夹中的任何文件进行更改是最糟糕的主意 –

0

重命名RegisterController任何其他南e或删除它:-)

0

这看起来很容易!您只需要覆盖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); 
} 
1

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

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