繁体   English   中英

PHP正则表达式从字符串中提取控制器和动作

[英]PHP regex to extract controller and action from string

因此,我正在开发一个Symfony2应用程序。 如果您知道我在说什么,那么您会知道那里的路由结构如何,因此我需要提取路由及其控制器和操作。

我找到了如何提取路线对象的方法,但是我无法提取控制器和动作,但是我有包含它们的字符串。

所以我有这个字符串: Acme\\SomeBundle\\Controller\\DefaultController::indexAction

所有字符串都具有相似的结构。 所有字符串都以ControllerName :: ActionName结尾。 控制器名称始终以“ Controller”结尾,动作名称始终以“ Action”结尾。 所以我无法弄清楚如何从字符串中提取这些值。 我是新来的正则表达式的新手,但我不知道如何执行此操作。 请帮助我,任何帮助:)

示例字符串:

`Emca\AnotherBundle\Controller\SecurityController::loginAction`

输出示例:

SecurityController
loginAction

我认为您不能仅使用一个正则表达式来做到这一点,但两个就足够了。 谢谢!

在symfony的ControllerNameParser类中可以找到一个示例:

$controller = 'Acme\YourBundle\Controller\SecurityController::loginAction';

if (0 === preg_match('#^(.*?\\\\Controller\\\\(.+)Controller)::(.+)Action$#', $controller, $match)) {
    throw new \InvalidArgumentException(sprintf('The "%s" controller is not a valid "class::method" string.', $controller));
}

$className      = $match[1];
$controllerName = $match[2];
$actionName     = $match[3];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM