2015-05-04 145 views
0

有哪些選項可用來阻止用戶將現有路線名稱用作帳戶名稱。Laravel路線和路線用戶名

例如: 我有一條路線,顯示最新帖子domain.com/latest。我還想簡化用戶的個人資料頁面網址,如下面的domain.com/a_user

當用戶使用latest用戶名註冊時,會發生此問題。這取決於路線順序,但我想拒絕這些用戶名。是否有任何簡單的驗證,或者我應該將所有基本路線存儲在一個數組中,並檢查它?

+0

另一種方法是使用子域名路由:http://laravel.com/docs/5.0/routing#route-groups。也看起來更清潔IMO :) – Dencker

回答

3

您可以使用not_in與一系列路徑路徑結合使用,您可以使用Route::getRoutes()。 所以你的情況,你可以有這樣的事情:

$routes = []; 

// You need to iterate over the RouteCollection you receive here 
// to be able to get the paths and add them to the routes list 
foreach (Route::getRoutes() as $route) 
{ 
    $routes[] = $route->getPath(); 
} 

$validator = Validator::make(
    ['username' => $username], 
    ['username' => 'not_in:' . implode(',', $routes)] 
);