php-mvc icon indicating copy to clipboard operation
php-mvc copied to clipboard

Pass more than one parameter

Open GuiHSilva opened this issue 4 years ago • 11 comments

I know it is possible for me to pass a parameter to an action as follows: $router->add('{controller}/{action}/{id:\d+}');

public function indexAction() { View::renderTemplate('Sobre/index.html'); }

But how can i pass, two or more parms to the router and get there parms into the action.

Sorry my bad english.

GuiHSilva avatar Aug 27 '20 16:08 GuiHSilva

You can have as many variables as you like in a route. You can then get them in the route_params property. For example:

$router->add('{controller}/{action}/{id:\d+}/{slug:\w+}');

Then in the action:

public function indexAction()
{
   $id = $this->route_params['id'];
   $slug = $this->route_params['slug'];
}

Is that what you meant?

daveh avatar Aug 27 '20 17:08 daveh

Could you not pass an array?

vince844 avatar Aug 28 '20 12:08 vince844

that's what I meant, thank you very much

GuiHSilva avatar Aug 28 '20 13:08 GuiHSilva

Could you not pass an array?

I'm afraid not, that's not how the framework works at the moment with parameters.

daveh avatar Aug 28 '20 15:08 daveh

Could you not pass an array?

I'm afraid not, that's not how the framework works at the moment with parameters.

Hi @daveh will it be possible with the new version? Many thanks

vince844 avatar Aug 28 '20 19:08 vince844

@vince844 I'm not sure what you mean - each URL segment (e.g. /segment1/segment2 etc.) can correspond to a variable, so you could have something like this: /users/123/orders/345/product-1 and so on. As the URL is a string, each segment can only ever be a simple string too. If you want several values, then just have several route variables / segments.

daveh avatar Aug 29 '20 15:08 daveh

I want to update the data .So I tried to pass it with the traditional system of php (i.e list?id=1)...than i am not able to get this id by using $_GET method.

jainamachintyalabs avatar Oct 13 '20 07:10 jainamachintyalabs

If you're updating data, you should really be using POST. If you have values in a form using the POST method, you can get them in the $_POST array.

daveh avatar Oct 13 '20 13:10 daveh

I have this route: $router->add("product/{id}/{slug}","ProductController@index");

I always pass id but slug is optional how can I define the optional parameter?

mosafer72 avatar Oct 29 '20 05:10 mosafer72

@mosafer72 You can't do that with a single route in this framework, you'd have to add two routes:

$router->add('product/{id}/{slug}', ['controller' => 'Product',  'action' => 'index']);
$router->add('product/{id}', ['controller' => 'Product',  'action' => 'index']);

Plus, the controller and action are specified as individual array elements above, not as in your example (unless you've modified the router)

daveh avatar Oct 29 '20 11:10 daveh

Take a look at this pull request #73 to allow one or more parameters, without need to create a route for every method. I think can be useful for most simple requests.

WKnak avatar Nov 19 '20 09:11 WKnak