Welcome Guest, Not a member yet? Register   Sign In
Problem with routers, several parameters
#1

Hello!

I dont understand how I can pass several parameters to index function in routes, for example, I want to do this:
$routes->add('en/specials/{:any}', 'Products::index/en/specials/$1');
$routes->add('fr/specials/{:any}', 'Products::index/fr/specials/$1');

But its dont work, redirect to 404 error page.

This is work fine:
$routes->add('specials/{:any}', 'Goods::index/specials/$1');

But I need language parameter in address string. Tell me please what am I doing wrong.
Reply
#2

The "to" portion of your array is confusing it. It expects the classname::methodname of the class/method handling that route. In your case it thinks the language codes are the name of the method, not specials which is what I'm assuming is the actual method?

If you need the language code used in the method, you could write it like this:

```
$routes->get('en/specials/{:any}', 'Products::index/specials/en/$1');
```

The language code is the first parameter sent and the name/id/whatever of the special is the second parameter. If you're only using the language code during a controller filter to set something site-wide, you could ignore that altogether and just pass in $1.

A couple words of warning here.

1. Don't use add as the method. Instead, use the correct HTTP verbs (get, post, etc). It's more flexible and safer. If you need to match both get and post, use match(['get', 'post']...).

2. You might get something unexpected using (:any) now. Unlike in CI3, any will accept...well, anything, not just a single segment. If your special codes have a forward slash in them sometimes, then "any" is correct. If you need to handle other segments after that, though, use (Confusedegment) instead.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB