Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 4:创建默认路线_Php_Laravel_Laravel 4 - Fatal编程技术网

Php Laravel 4:创建默认路线

Php Laravel 4:创建默认路线,php,laravel,laravel-4,Php,Laravel,Laravel 4,我正在尝试在我的Laravel4RESTAPI中创建一个默认路由,当我定义的其他路由都与请求不匹配时,它会向调用者返回一个特定的错误 这可能吗?不幸的是,我在文档中找不到任何内容,因此我尝试使用通配符(*)作为我的routes.php中的最后一个路由定义,但这不起作用 Route::when("*", function(){ throw new CustomizedException('Route not found...'); }); 当我使用此路线并执行artisan路线时,我得到

我正在尝试在我的Laravel4RESTAPI中创建一个默认路由,当我定义的其他路由都与请求不匹配时,它会向调用者返回一个特定的错误

这可能吗?不幸的是,我在文档中找不到任何内容,因此我尝试使用通配符(
*
)作为我的
routes.php
中的最后一个路由定义,但这不起作用

Route::when("*", function(){
    throw new CustomizedException('Route not found...');
});
当我使用此路线并执行
artisan路线
时,我得到一个异常:
{“error”:{“type”:“ErrorException”,“message”:“类闭包对象无法转换为字符串”,“file”:“\/Applications\/MAMP\/htdocs\/activingtools\/vendor\/laravel\/framework\/src\/illumb\/Foundation\/Console\/routeCommand.php”,“line”:153}

调用不存在的路由不会触发自定义异常,但会触发标准异常:
{“error”:{“type”:“Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException”,“message”:“file”:“\/Applications\/MAMP\/htdocs\/activingtools\/vendor\/laravel\/framework\/src\/light\/Routing\/Router.php”,“line”:1429}

我也尝试过按建议使用
any
,但也不起作用:

Route::any( '(.*)', function( $page ){
    throw new ValidationException('Custom error');
});
当我调用不存在的路由时,此路由也不会触发


任何提示,我做错了什么都将不胜感激。

试试这样的方法

Route::get('{slug}', function($slug) {
    // get the page from database using Page model
    $page = Page::where('slug', '=', $slug)->first();

    if ( is_null($page) ) {
        return App::abort(404);
    }

    return View::make('page')->with('page',$page);
});

// Show 404 Page

App::missing(function($exception)
{
    return Response::view('errors.missing', array(), 404);
});

如果没有匹配的路由,Laravel将抛出“Symfony\Component\HttpKernel\Exception\NotFoundHttpException”。您只需编写自己的错误处理程序来捕获该异常并执行其他操作(请看一看)

添加以下两个块中的一个(例如,在“应用程序错误处理程序”块中的“app/start/global.php”中):

或:


我花了一段时间才弄明白,实际上@devo非常接近:

Route::get('{slug}', function($slug) {

    // check your DB for $slug, get the page, etc...

})->where('slug', '^.*');

这也会抓住机会。如果您希望单独处理主页,请将regexp更改为:where('slug','^.+')

是否尝试将其放在路由文件的末尾

Route::any( '/{default?}', function( $page ){
    throw new ValidationException('Custom error');
});


这只适用于像/foo这样的简单URL。如果你有/foo/bar,它就抓不住了…我本来打算写同样的答案+1.为了节省我的时间:D
Route::get('{slug}', function($slug) {

    // check your DB for $slug, get the page, etc...

})->where('slug', '^.*');
Route::any( '/{default?}', function( $page ){
    throw new ValidationException('Custom error');
});
Route::any('/{default?}', 'TestController@yourMethod'); // pointing to a method displaying a 404 custom page