在laravel 5.2中,我想将所有未定义的url路由到一个特定的控制器。
我正在开发类似CMS的功能,我想要这个东西。
Route::get('profile', 'Controller@profile');
Route::get('{any}', 'Controller@page');
so url like
www.domain.com/post/po-t/some/thing www.domain.com/profile所以第一个url应该重定向到页面函数,第二个url应该重定向到配置文件函数
基本上我想要一些n个数或参数的想法,在页面中它可以是任意数量的参数,如"www.domain.com/post/po-t/some/thing"
路由
Route::get('{any}', 'Controller@page');
只适用于像
这样的url www.domain.com/post
如果你想让它有更多的选项,你必须创建另一个路由,比如
Route::get('{any}/{any1}', 'Controller@page');
这将适用于两个选项,如回调
www.domain.com/post/asdfgd
未定义路由生成404 HTTP状态。您可以在resources/views/errors
上创建404.blade.php
页面,放置您想要显示的任何视图。当出现404错误时,它会将您重定向到该页面。您不需要做任何其他事情,laravel会在幕后处理其余的事情。
使用中间件
在handle方法中,您可以访问$request
对象。当没有找到路由时,重定向到你的后备路线。关于获取当前url
编辑:可以在laracast论坛中找到实现。发布者想要保护管理员路由:
public function handle($request, Closure $next)
{
$routeName = Route::currentRouteName();
// isAdminName would be a quick check. For example,
// you can check if the string starts with 'admin.'
if ($this->isAdminName($routeName))
{
// If so, he's already accessing an admin path,
// Just send him on his merry way.
return $next($request);
}
// Otherwise, get the admin route name based on the current route name.
$adminRouteName = 'admin.' . $routeName;
// If that route exists, redirect him there.
if (Route::has($adminRouteName))
{
return redirect()->route($adminRouteName);
}
// Otherwise, redirect him to the admin home page.
return redirect('/admin');
}