0

In a Zend application with an example url like this one:

http://example.com/Controller/action/42

Is there any convenient way to retreive that last parameter? (The "42")

$this->_request->getParams();

won't work since it only retreives name value pairs.

1
  • Have you tried using $this->_getAllParams()? Commented Sep 19, 2016 at 11:56

3 Answers 3

6

It looks like you're looking for Zend_Controller_Router.

Zend_Controller_Router_Route is the standard framework route. It combines ease of use with flexible route definition. Each route consists primarily of URL mapping (of static and dynamic parts (variables)) and may be initialized with defaults as well as with variable requirements.

$route = new Zend_Controller_Router_Route(
    ':controller/:action/:id',
    array(
        'controller' => 'index',
        'action'     => 'index',
        'id'         => 0
    ),
    array('id' => '\d+') // Makes sure :id is an int
);

$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute('myRouteName', $route);
Sign up to request clarification or add additional context in comments.

Comments

2

its not valid zf url

url is usually consist of

/controller/action/id/value/id2/value2/....../idN/valueN

and then can read all of these params together :

$this->_getAllParams()

$this->_request->getParams();

or by its ID using your own

$this->_request->getParam("id");

$this->_getParam("id")

1 Comment

This is not entirely true. It is not a STANDART ZF url, but it certainly is valid!
0

First of all, as @adlawson, state you need to create a route that accept the parameter. By doing this you also give a name to this parameter. The code @adlawson proposed is good enough:

$route = new Zend_Controller_Router_Route(
':controller/:action/:id',
array(
    'controller' => 'index',
    'action'     => 'index',
    'id'         => 0
),
array('id' => '\d+') // Makes sure :id is an int
);

$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute('myRouteName', $route);

Then, the simplest way, in the controller, to retrieve the value of the id from your url http://example.com/Controller/action/42 is the following :

public function indexAction () {
    ...
    $id = $this->params()->fromRoute('id');
    ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.