Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass more than one parameter #64

Open
GuiHSilva opened this issue Aug 27, 2020 · 11 comments
Open

Pass more than one parameter #64

GuiHSilva opened this issue Aug 27, 2020 · 11 comments

Comments

@GuiHSilva
Copy link

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.

@daveh
Copy link
Owner

daveh commented Aug 27, 2020

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?

@vince844
Copy link

Could you not pass an array?

@GuiHSilva
Copy link
Author

that's what I meant, thank you very much

@daveh
Copy link
Owner

daveh commented Aug 28, 2020

Could you not pass an array?

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

@vince844
Copy link

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

@daveh
Copy link
Owner

daveh commented Aug 29, 2020

@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.

@jainamachintyalabs
Copy link

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.

@daveh
Copy link
Owner

daveh commented Oct 13, 2020

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.

@mosafer72
Copy link

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?

@daveh
Copy link
Owner

daveh commented Oct 29, 2020

@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)

@WKnak
Copy link

WKnak commented Nov 19, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants