1

I've encounterd a problem with URL creation.

In my plugin I have the following.

public function registerSiteRoutes() {
    return [
        'korting/zoeken' => array('action' => '/discount/discount/index'),
        'korting/(?P<id\d+>)-(?P<slug>[a-zA-Z0-9-]+)' => array('action' => 'discount/discount/view',),
    ];
}

Now when I'm at the frontend, I use the following to create the URL :

{{ url('/discount/discount/view',{id:entry.id,slug:entry.slug}) }}

My plugin gets its data from a solarium document, so its no entry.

I would expect that my URL would become neat like :

/korting/124-test-1

but instead I get :

/korting?id=124&slug=test-1

This feature is build in by default in the Yii URL manager, but Craft doesn't make use of it. This is resulting in my problem that Craft doesn't have the abbility to make pathed URLs.

Is there any other way for me to do this?

2

You can use the ~ (concatenate string) operator to build the URL manually:

{{ url('/discount/discount/view/'~entry.id~'-'~entry.slug) }}
2
  • if i concat the url it doesnt get translated to /korting/id-slug but it will stay /discount/discount/view/id/slug wich u can see isnt pretty.
    – Terraskye
    Apr 1 '15 at 13:50
  • I wasn't aware that the UrlHelper class would actually translate action URLs to site routes. Never seen that happen, and can't really pin down where it happens looking through the UrlHelper class. Do you have to do something somewhere to trigger that behaviour? Tried testing out your above code, but PHP choked on the regex in your routes. In any case – I guess just using {{ url( 'korting/'~entry.id~'-'~entry.slug) }} isn't an option? :) Apr 1 '15 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.