<i id='Ypb3p'><tr id='Ypb3p'><dt id='Ypb3p'><q id='Ypb3p'><span id='Ypb3p'><b id='Ypb3p'><form id='Ypb3p'><ins id='Ypb3p'></ins><ul id='Ypb3p'></ul><sub id='Ypb3p'></sub></form><legend id='Ypb3p'></legend><bdo id='Ypb3p'><pre id='Ypb3p'><center id='Ypb3p'></center></pre></bdo></b><th id='Ypb3p'></th></span></q></dt></tr></i><div id='Ypb3p'><tfoot id='Ypb3p'></tfoot><dl id='Ypb3p'><fieldset id='Ypb3p'></fieldset></dl></div>
  • <small id='Ypb3p'></small><noframes id='Ypb3p'>

    <legend id='Ypb3p'><style id='Ypb3p'><dir id='Ypb3p'><q id='Ypb3p'></q></dir></style></legend>

      • <bdo id='Ypb3p'></bdo><ul id='Ypb3p'></ul>
      1. <tfoot id='Ypb3p'></tfoot>

        Laravel - 对所有路线使用 (:any?) 通配符?

        时间:2023-10-30

                <tfoot id='PNikV'></tfoot>
                  <tbody id='PNikV'></tbody>
                <i id='PNikV'><tr id='PNikV'><dt id='PNikV'><q id='PNikV'><span id='PNikV'><b id='PNikV'><form id='PNikV'><ins id='PNikV'></ins><ul id='PNikV'></ul><sub id='PNikV'></sub></form><legend id='PNikV'></legend><bdo id='PNikV'><pre id='PNikV'><center id='PNikV'></center></pre></bdo></b><th id='PNikV'></th></span></q></dt></tr></i><div id='PNikV'><tfoot id='PNikV'></tfoot><dl id='PNikV'><fieldset id='PNikV'></fieldset></dl></div>

                <legend id='PNikV'><style id='PNikV'><dir id='PNikV'><q id='PNikV'></q></dir></style></legend>
              • <small id='PNikV'></small><noframes id='PNikV'>

                • <bdo id='PNikV'></bdo><ul id='PNikV'></ul>

                  本文介绍了Laravel - 对所有路线使用 (:any?) 通配符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的路由有点问题.

                  我正在开发 CMS,我需要两条主要路线./admin/(:any).admin 控制器用于路由 /admin,而 view 控制器应该用于除 /admin.从 view 控制器,然后我将解析 url 并显示正确的内容.

                  I'm working on a CMS, and I need two primary routes. /admin and /(:any). The admin controller is used for the route /admin, and the view controller should be used for anything else than /admin. From the view controller, I will then parse the url and show the correct content.

                  这就是我所拥有的:

                  Route::get(array('admin', 'admin/dashboard'), array('as' => 'admin', 'uses' =>'admin.dashboard@index'));
                  Route::any('(:any)', 'view@index');
                  

                  第一条路线有效,但第二条路线无效.我玩了一下,似乎如果我使用不带问号的 (:any),它只有在 / 后面放一些东西才有效.如果我把问号放在那里,它根本不起作用.

                  The first route works, but the second one doesn't. I played around with it a little bit, and it seems if I use (:any) without the question mark, it only works if I put something after /. If i do put the question mark there, it doesn't work at all.

                  我希望以下所有路线都转到 view@index:

                  I want all of the following routes to go to view@index:

                  /
                  /something
                  /something/something
                  /something/something/something
                  /something/something/something/something
                  ...etc...
                  

                  如果不硬编码一堆 (:any?)/(:any?)/(:any?)/(:any?) (我什至不知道有效),这是否可能)?

                  Is this possible without hardcoding a bunch of (:any?)/(:any?)/(:any?)/(:any?) (which I don't even know works)?

                  最好的方法是什么?

                  推荐答案

                  自 Laravel 4 发布以来,关于这个话题一直存在一些混淆,这个答案是针对 Laravel 3 的.

                  There has been some confusion since the release of Laravel 4 regarding this topic, this answer was targeting Laravel 3.

                  有几种方法可以解决这个问题.

                  There are a few ways to approach this.

                  第一种方法是匹配(:any)/(:all?):

                  Route::any('(:any)/(:all?)', function($first, $rest=''){
                      $page = $rest ? "{$first}/{$rest}" : $first;
                      dd($page);
                  });
                  

                  不是最好的解决方案,因为它被分解为多个参数,并且由于某种原因 (:all) 无法自行工作(错误?)

                  Not the best solution because it gets broken into multiple parameters, and for some reason (:all) doesn't work by itself (bug?)

                  第二种解决方案是使用正则表达式,我认为这是比上面更好的方法.

                  The second solution is to use a regular expression, this is a better way then above in my opinion.

                  Route::any( '(.*)', function( $page ){
                      dd($page);
                  });
                  

                  还有一种方法,即使路由可能匹配其他模式,也可以让您检查是否有 cms 页面,前提是这些路由返回 404.此方法修改 routes.php 中定义的事件侦听器:

                  There is one more method, which would let you check if there are cms pages even when the route may have matched other patterns, provided those routes returned a 404. This method modifies the event listener defined in routes.php:

                  Event::listen('404', function() {
                      $page = URI::current();
                      // custom logic, else
                      return Response::error('404');
                  });
                  

                  但是,我的首选方法是 #2.我希望这有帮助.不管你做什么,确保你在这些之上定义了所有其他路由,捕获所有路由,之后定义的任何路由都不会触发.

                  However, my preferred method is #2. I hope this helps. Whatever you do, make sure you define all your other routes above these catch all routes, any routes defined after will never trigger.

                  这篇关于Laravel - 对所有路线使用 (:any?) 通配符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                    <bdo id='lxQsd'></bdo><ul id='lxQsd'></ul>
                  • <tfoot id='lxQsd'></tfoot>

                    <small id='lxQsd'></small><noframes id='lxQsd'>

                      • <legend id='lxQsd'><style id='lxQsd'><dir id='lxQsd'><q id='lxQsd'></q></dir></style></legend>
                      • <i id='lxQsd'><tr id='lxQsd'><dt id='lxQsd'><q id='lxQsd'><span id='lxQsd'><b id='lxQsd'><form id='lxQsd'><ins id='lxQsd'></ins><ul id='lxQsd'></ul><sub id='lxQsd'></sub></form><legend id='lxQsd'></legend><bdo id='lxQsd'><pre id='lxQsd'><center id='lxQsd'></center></pre></bdo></b><th id='lxQsd'></th></span></q></dt></tr></i><div id='lxQsd'><tfoot id='lxQsd'></tfoot><dl id='lxQsd'><fieldset id='lxQsd'></fieldset></dl></div>

                          <tbody id='lxQsd'></tbody>