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

2023-10-30php开发问题
0

本文介绍了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?) 通配符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17