php-mvc
php-mvc copied to clipboard
Pass more than one parameter
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.
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?
Could you not pass an array?
that's what I meant, thank you very much
Could you not pass an array?
I'm afraid not, that's not how the framework works at the moment with parameters.
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 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.
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.
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.
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 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)
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.