【发布时间】:2016-04-22 05:36:40
【问题描述】:
我需要一个 laravel routes.php 条目,它可以捕获网站特定 domain.com/premium-section 的所有流量,以便在访问高级内容之前提示人们成为会员。
【问题讨论】:
我需要一个 laravel routes.php 条目,它可以捕获网站特定 domain.com/premium-section 的所有流量,以便在访问高级内容之前提示人们成为会员。
【问题讨论】:
您还可以通过在参数上使用正则表达式来捕获“全部”。
Route::group(['prefix' => 'premium-section'], function () {
// other routes
...
Route::get('{any}', function ($any) {
...
})->where('any', '.*');
});
如果没有使用可选参数定义路由,也可以捕获整个组。
Route::get('{any?}', function ($any = null) {
...
})->where('any', '.*');
最后一个也会捕获“domain.com/premium-section”。
【讨论】:
Route:any('*')!
Route::get('/{action}', 'SiteController@defaultPageHandler')->where('action', '.*'); 会更好;)
Route::fallback 会更好,因为它只是一个捷径。
Route::fallback 不是闭包,所以可以缓存
这就是诀窍:
Route::any('/{any}', 'MyController@myMethod')->where('any', '.*');
【讨论】:
/ 路由,否则点击实际的根主页会导致 500 错误
? 添加到参数中,甚至会捕获根 uri 即:{any?}
但是没有任何匹配的方法,只是一个包罗万象的方法。
Route::group(['as' => 'premium-section::',
'prefix' => 'premium-section',
'middleware' => ['web']],
function(){
Route::any('', 'PremiumSectionController@premiumContentIndex');
Route::controller('/', 'PremiumSectionController');
});
.
namespace App\Http\Controllers;
use ...
class PremiumSectionController extends Controller{
public function premiumContentIndex(){
return 'no extra parameters';
}
//magically gets called by laravel
public function missingMethod($parameters = array()){
return $parameters;
}
}
【讨论】:
Laravel 现在有一个内置方法:
【讨论】:
这对我有用
// The catch-all will match anything except the previous defined routes.
Route::any('{catchall}', 'CatchAllController@handle')->where('catchall', '.*');
【讨论】: