在symfony中定义控制器/操作的默认路由 [英] Defining default routing for controller/action in symfony 2

查看:502
本文介绍了在symfony中定义控制器/操作的默认路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想让每一个url调用,除了在 / ExplicitControllerName / ExplicitActionToRun 之后定义的路径。



例如一些伪代码:

  default_pathing:
pattern:/ {controller } / {action}
defaults:{_controller:Bundle:Default:index}



如果我转到
www.example.com/Page/About



它会调用我的控制器

  class Page extends Controller 
{
public AboutAction()
{
//由上述URL调用
}
}

此问题未回答:Symfony2 / routing /将参数用作控制器或操作名称



想象一下,我有100页,许多子路由页面每次都做同样的路由。我想为所有这100个控制器做1路由。我们将如何做到这一点?



PS我真的要去像C#.NET MVC 4.0路由,它允许您为典型的设置设置路由你可能有,即使至少它的发展

解决方案

你的问题不是完全清楚,但这里有一些提示。 / p>

我可以想象你想要解决的两个用例:



1)某些类型的CMS页面,像你的例子,这些页面没有太多的逻辑,只是渲染一些视图,在这种情况下你会像:

  class CMSPageController 
{
public function renderPage($ pageSlug)
{
$ page = $ this-> cmsPageRepository-> findBySlug );

//渲染视图的逻辑
}
}

和相应的路由配置:

 < route id =cms_page_viewpattern =/ cms / pageSlug}> 
< default key =_ controller> cms_page.controller.page:renderPage< / default>
< requirement key =_ method> GET< / requirement>
< requirement key =slug> [0-9a-zA-Z \-\.\ /] +< / requirement>
< / route>

2)你有更复杂的需求,和/或按照特定的模式命名控制器/操作,因此您需要编写自定义 UrlMatcherInterface 实现。查看本机实现知道从哪里开始。这将允许您定义后备。


If I wanted to make it so that every url call apart from ones I have defined after act upon /ExplicitControllerName/ExplicitActionToRun... how might the routing look like.

for example some pseudo code:

default_pathing:
    pattern:  /{controller}/{action}
    defaults: { _controller: Bundle:Default:index }

So if I went to www.example.com/Page/About

it would call my controller

class Page extends Controller
{
    public AboutAction()
    {
        // Called by above URL
    }
}

This question does not answer: Symfony2 / routing / use parameters as Controller or Action name

Imagine I have 100 pages with lots of sub routing pages doing pretty much the same routing every time. I want to do 1 routing for all those 100 controllers. How would we do this?

P.S I'm really going for something like the C#.NET MVC 4.0 routing in which it allows you to set a routing for a typical setup you might have even if at the very least its for development

解决方案

Your question is not totally clear but here are some hints.

I can imagine two use cases you're trying to solve:

1) You've a lot of some sort of CMS page, like your about example, these pages don't have much logic and just render some view, in such case you would something like:

class CMSPageController
{
    public function renderPage($pageSlug)
    {
        $page = $this->cmsPageRepository->findBySlug($pageSlug);

        // your logic to render the view
    }
}

And the according routing configuration:

<route id="cms_page_view" pattern="/cms/{pageSlug}">
  <default key="_controller">cms_page.controller.page:renderPage</default>
  <requirement key="_method">GET</requirement>
  <requirement key="slug">[0-9a-zA-Z\-\.\/]+</requirement>
</route>

2) You have much more complex requirements, and/or follow a specific pattern to name your controller/action, therefore you need to write a custom UrlMatcherInterface implementation. Take a look at the native implementation to know where to start. It would allow you define a fallback.

这篇关于在symfony中定义控制器/操作的默认路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆