简体   繁体   English

在路由中设置url参数(Symfony 2.6)

[英]Set url parameter in route (Symfony 2.6)

So, lets assume i got this showAction() in my DisplayController.php : 因此,假设我在DisplayController.php得到了showAction()

/**
 * @param $type
 * @param $slug
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function showAction($type, $slug)
{ 
    ...
}

Usually, the following route links to this action: 通常,以下路线链接到此操作:

my_bundle_display_show:
    pattern: /display/{type}/{slug}
    defaults: { _controller: MyFunnyBundle:Display:show }

So, when I request my-website.com/display/product/A , everything works as intended. 因此,当我请求my-website.com/display/product/A ,一切都会按预期进行。

However, now I need to implement a quicklink which requires me to skip the type argument which looks like my-website.com/specific-product which is supposed to link to my-website.com/display/product/specific-product . 但是,现在我需要实现一个快速链接,该链接要求我跳过看起来像my-website.com/specific-producttype参数,该参数应该链接到my-website.com/display/product/specific-product The route i created for that looks like that: 我为此创建的路由如下所示:

my_bundle_display_show_specific_product:
    pattern: /{slug}
    defaults: { _controller: MyFunnyBundle:Display:show }
    requirements:
        slug: "specific-product"
    defaults:
        type: "product"

The specific error messages is Controller "MyBundle\\Controller\\DisplayController::showAction()" requires that you provide a value for the "$type" argument (because there is no default value or because there is a non optional argument after this one). 特定的错误消息是Controller "MyBundle\\Controller\\DisplayController::showAction()" requires that you provide a value for the "$type" argument (because there is no default value or because there is a non optional argument after this one).

However, that doesn't work since I'm required to add a $type in order for it to work. 但是,这不起作用,因为我需要添加$type才能使其正常工作。 I could create a new showSpecificProductAction , but I don't want to do that since both functions basically do the same. 我可以创建一个新的showSpecificProductAction ,但是我不想这样做,因为这两个函数基本上都相同。 So I was wondering if i can "set" variables within routes so I can basically only make $slug an actual variable without editing the showAction() itself? 所以我想知道是否可以在路由内“设置”变量,这样我基本上只能使$slug成为实际变量,而无需编辑showAction()本身?

在Controller动作中,将功能更改为public function showAction($type='product', $slug)

Do not search for easier/shorter solutions. 不要搜索更简单/更短的解决方案。 Make thing correctly and you won't have problems in future. 正确做事,以后不会有任何问题。

First, define your routing logic/aim. 首先,定义您的路由逻辑/目标。 If you want a short url to redirect you to a full page, than do exactly this. 如果您想使用短网址将您重定向到整个页面,则可以这样做。 You could do this in yaml right away with: 您可以立即在yaml中执行以下操作:

my_bundle_display_show_specific_product:
    path: /{slug}/
    defaults:
        _controller: FrameworkBundle:Redirect:redirect
        route: my_bundle_display_show
        permanent: true
        type: "some default value"

If you don't know the default type value, just create new action in controller, find the value and redirect from controller providing this value and using proper HTTP code. 如果您不知道默认类型值,只需在controller中创建新操作,找到该值并从提供该值并使用正确的HTTP代码的controller重定向。 Such redirects are good for SEO as well: 这样的重定向对SEO也是有好处的:

public function actionOne($slug) {
    $type = ... // find out the type
    return $this->redirectToRoute('actionTwo', ['type' => $type, 'slug' => $slug], 301);
}

public function actionTwo($type, $slug) {
    // do some stuff...
}

If you DO need both urls to work, just create 2 routes configuration and 2 actions. 如果您确实需要两个网址都可以工作,则只需创建2个路由配置和2个操作即可。 But since they have common logic, create a private method in controller, which will do the logic for both. 但是,由于它们具有通用逻辑,因此请在控制器中创建一个私有方法,这将为两个逻辑都做。 This is what OOP is all about: 这就是OOP的全部意义:

public function actionOne($slug) {

    $type = ... // find out the type

    return $this->renderProduct($type, $slug);
}

public function actionTwo($type, $slug) {
    return $this->renderProduct($type, $slug);
}

private function renderProduct($type, $slug) {
     return $this->render('view', [...]);
}

Think of a logic first, then separate logical parts (using route/controller/action). 首先考虑逻辑,然后考虑单独的逻辑部分(使用路由/控制器/动作)。 This is what it is all about. 这就是全部。 Good luck! 祝好运!

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

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