In a Zend application with an example url like this one:
http://example.com/profile/423423(some id)
How do I get param from url?
I try to use:
$this->getRequest()->getParam('action');
But I get action does not exist.
then I try something like this:
protected function _initRouter()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute('profile/:catid', new Zend_Controller_Router_Route('profile/:catid',
array(
'module' => 'default',
'controller' => 'profile',
'action' => 'index' // Check your action and controller
)));
}
How do I get param from url?.
You can use the getParam() methods to retrieve request variables like route, GET and POST.
Inside your controllers action you can do this.
$this->getParam('parameterName'); or $this->getRequest()->getParam('parameterName');
If you want to get the name of action and controller that matched the current route you need to use following methods.
$this->getRequest()->getActionName() and $this->getRequest()->getControllerName()
You should just use
$this->getRequest()->getParam('profile');
from your controller.
You shouldn't need to use a custom route.
Kind regards
Garry
EDIT
I have just noticed that you wish to use the profile controller. The simplest solution would be to add an id into your url like.
http://example.com/profile/id/423423
and get the profile id with
$this->getRequest()->getParam('id');
Related
I have a controller which I want to use for many routes. I need to pass some parameters, but I don't know how to do that without using closures.
I have an action like this:
public function show($view, $param)
{
return View::make($view)->with('param', $param);
}
Now I know I can generate a route like this:
Route::get('/myfirstlink', array('uses' => 'MyController#show') );
but I want to pass $view and $param without passing them in the url.
Something like:
Route::get('/myfirstlink', array('uses' => 'MyController#show') ); //with $view='firsttemplate',$param='firstparam'
Route::get('/mysecondlink', array('uses' => 'MyController#show') ); //with $view='secondtemplate',$param='secondparam'
How to do that in the cleanest way?
Thank you in advance
Edit for clarification:
I don't need the user to specify values. I want to call the same controller action with different parameters... something like this:
Route::get('/myfirstlink', array('uses' => 'MyController#show', 'atts' => array('view'=>'firsttemplate','param'=>'firstparam')) );
You can create an session, or send via input hidden, or a cookie.
But in your case i recommend use sessions, you can destroy/change/create it anytime.
How do you add in the yii form the action for PHP_SELF, I am currently using this:
'action' => '/site/contact',
but I want to replace the action to call itself again. How do I do that?
UPDATE:
tried removing the action in the array, now it does not go into validating the form.
Use Yii controller method createUrl - calling it without params will point to current controller and action.
In views you can just call $this->createUrl():
'action' => $this->createUrl(),
Important: do not hardcode actions like in your question. Always call createUrl, this will ensure proper urls if you change url rules:
'action' => $this->createUrl('otherController/someAction'),
or same controller different action
'action' => $this->createUrl('otherAction'),
etc.
Do mean perhaps a page refresh?
Just do $this->refresh() inside your controller action.
My route setup :
Zend_Controller_Front::getInstance()
->getRouter()
->addRoute('view', new Zend_Controller_Router_Route('controller/action/:name'))
My link in view :
$this->url(array("name" => "John"), "view", TRUE);
// returns "controller/action/John" as should
Now, when I am at controller/action/John, how do I get the name from URL ? I tried
$this->getRequest()->getParam("name");
but the name param isn't there - getRequest() returns only controller, action and module params.
When you set up your route configuration the route definition should either directly match the controller/action names or be set with defaults. Actually setting the defaults in any case is just good practice and helps you avoid issues like this.
So, in your case according to the comments your route should probably look like this.
$defaults = array(
'controller'=> 'offers',
'action' => 'view',
'name' => ''
);
$route = new Zend_Controller_Router_Route('offers/view/:name',$defaults);
As mentioned in the comments you can always check what route has been used with Zend_Controller_Front::getInstance()->getRouter()->getCurrentRouteName(). If it doesn't show your expected route the Router isn't able to find a match and moves on until it usually ends in the "default" route.
As a side note to your question: When you use $this->url(array("name" => "John"), "view", TRUE) you only create the link based on the route. This method is only part of the view and does nothing in terms of dispatching to a controller or action.
For those who found this question and for future reference, you can get params from route using this : $this->params()->fromRoute('param1', 0); in Zend Framework 2 at least. This is what I was looking for in this question.
Im using "Zend form". What should I do to get correct "action" parameter in my FORM tag?
For example, the form Form_Search used in SearchController in indexAction. Where and how should I set the action parameter to form to make it "/my/app/directory/search/index"?
Update: "action" parameter should be the same as the form will assume if not set. I just need the param in FORM to simplify JS coding.
You can use setAction method of Zend_Form, e.g.:
$yourForm->setAction('/my/app/directory/search/index');
Hope this is what you are looking for.
EDIT:
You can retrieve many info about the request, in an action, using $this->getRequest() method that returns an instance of Zend_Controller_Request_Http. For example, if to get basePath and everything between the BaseUrl and QueryString you can do:
$infoPath = $this->getRequest()->getPathInfo();
$basePath = $this->getRequest()->getBaseUrl();
$yourForm->setAction($basePath . $infoPath);
Zend_Controller_Request_Http has other methods that might be useful to you.
You can use the url() view helper to create your action urls.
$view->url(array(
'module' => 'mymodule',
'controller' => 'mycontroller',
'action' => 'myaction',
'param1' -> 'myvalue1',
// etc
), 'default');
// 'default' here refers to the default route.
// If you specify a custom route, then that route will be used to
// assemble() the url.
Depending where you call $form->setAction(), you can access to the $view in different ways.
In the controller: $this->view
In the form itself: $this->getView()
In a view script: $this
In view helper extending Zend_View_Helper_Abstract: $this->view
Hope it helps!
Just an addition to previous answers. Kind of "industry standard approach" is to render/validate/process the form all on the same page. Then there is no need to set the action as it's filled with current URL.
Exception is for example serch field in layout. Then use $form->setAction($url). But always remember to echo the form in the search action. If the form value is invalid, ZF would expect you to render it in page with the errors. Or you can use $form->getErrors().
`$this->setAttribs(array(
'accept-charset' => 'utf-8',
'enctype' => Zend_Form::ENCTYPE_URLENCODED,
'method' => 'get',
'action' => '/controller/action/param/attr'
))`
At the moment in my ZF project have a URL structure like this:
/news/index/news_page/1/blog_page/2
When I generate my pagination I use the URL helper as follows:
<?php echo $this->url(array('blog_page'=>3)); ?>
Which generates a URL like this:
/news/index/news_page/1/blog_page/3
What I'd like to do is use a custom route to have nicer URLs, something like this:
new Zend_Controller_Router_Route(
'news/:news_page/:blog_page',
array('controller' => 'news', 'action' => 'index')
);
However, when I try an use this route in the view helper:
<?php echo $this->url(array('blog_page'=>3), 'newsIndex'); ?>
It throws an error because I've not specified news_page in the params.
How can I get around this, and tell the url helper to use the 'current' values for these params?
The url helper will use the an existing parameter if it exists in the current request. It seems as if, in your particular case, the news_page param is not set in the request object. Setting a default value for the news_page parameter in your route should solve your problem.
So, your route definition should look something like this:
new Zend_Controller_Router_Route(
'news/:news_page/:blog_page',
array('controller' => 'news', 'action' => 'index', 'news_page' => 1)
);