dqnqpqv3841 2014-10-23 10:50
浏览 92

Laravel 4路由 - 如果不满足条件,则跳过路由的能力

So we have a load of content within the database, let's call these Articles. The paths for the Articles start from the application root and can contain slashes. So we want to search the DB to see if we match an Article, and if not skip the route and give other routes the opportunity to respond.

In Sinatra (which I believe has inspired the routing within Laravel) you have the option to pass to the next route. It might be this that's leading me astray.

Route::get( '{uri}', function( URI $uri ) {
  // check database, if we find a record we'll need to pass it to the appropriate controller
  // we'll have a class that handles this responsiblity

  // pass if we don't find anything
} )->where('uri', '.*');

Route::get( 'other/page', 'OtherController@sayHello' );

The issue Allow skip the route based on conditions #1899 talks about this, although I can't see how filters cater for this, they will simply intercept and give you an opportunity to stop route execution (throw exception, redirect to route specifically etc.), you can't return FALSE; without error. There is an example chaining a condition method to a route, although this method doesn't seem to exist (in 4.2).

Any ideas?

In addition to this, we're also are thinking about containing this logic within a package so it can be shared across applications, and wonder if you can influence the order of execution of routes provided by package?

  • 写回答

1条回答 默认 最新

  • dongxu7408 2014-10-23 11:05
    关注

    You can have a group of routes, which you contain within all the routes that match it. If it fails that group then it skips to the next section.

    Route::group(array('before' => 'auth'), function()
    {
        Route::get('/', function()
        {
            // Has Auth Filter
        });
    
        Route::get('user/profile', function()
        {
            // Has Auth Filter
        });   
    });
    

    http://laravel.com/docs/4.2/routing

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题