is there an easy way to access the url helpers from the models like the ones available in the controllers
i mean in the controllers there is an easy way to generate urls like this :
$this->_helper->url(controller,action,null,params);
now what i need is an easy way to pass urls direclty from the model to the views , for now what i am doing is to pass the CONTROLLER,ACTION AND PARAM as an array to controller then replace the text in the controller with with the helper url in the controller but i want a better way is there one?
You can access the url helper by calling it directly:
$urlHelper = new Zend_View_Helper_Url();
$urlHelper->url(array(),'',true);
The Model should not access the View, nor having to know about it.
If you have to do work that is related to the presentation layer, either use an Action Helper or a View Helper. The data you are processing is fully available in the Controller, so there should be no need to pass it from model.
actually it's a bit specific to my problem but i made work it this way
$check['msg'] == will contain the error or success message
from the models i pass the link that causes the problem
$messages['link'] = array('action'=>'index','controller'=>'trip','params'=>$tripid );
an on the controllers
$check['msg'] = str_replace('%link%',$this->_helper->url($check['link']['action'],$check['link']['controller'],null,array('id' => $check['link']['params'])),
$check['msg']);
$this->_flashMessenger->addMessage($check['msg']);
I found the following code snippet in a book, it maybe of help to someone:
$urlHelper = $this->_helper->getHelper('url');
$urlHelper->url(array(
'controller' => 'customer' ,
'action' => 'save'
),
'default'
);
Related
This is a real newbie question, but I have not used PHP and Phalcon very long and I am
sort of learning by studying examples, reading on internet and a bit of trial and error.
One thing that I got stuck on is how to pass variables to views that belongs to another controller.
If I want to pass a variable to a view in the same controller, lets call it showRoomController, then I simply use.
$this->view->setVar("id", $cars->id);
However, if I want to open the cars view from catalogueController, but from a page that belongs to showRoomController I use this:
return $this->forward("catalogue/cars");
How can I pass the cars id variable in the second example? Or do I need to use global variables?
I apologize if this is a very basic question that I probably should know.
Dispatcher's forward() method accepts params as well:
$this->dispatcher->forward(array(
"controller" => "myController",
"action" => "myAction",
"params" => array('name' => 'hello', 'surname' => 'world')
));
By default your view is a shared service in the DI. You can simply set parameters as you do in one controller, and when it did forward to another all of those parameters would still be there.
When you do $this->view in your controller it uses a magic method to get the view service from the DI, so if you do that in both controllers you will be referencing the same view.
In CakePHP have a bunch of unique URL names redirected in routes.php file.
Similar to this:
$beautiful_urls[0] = '/view/location-name/image-name.html';
Router::connect($beautiful_urls[0],
array('controller' => 'Foo','action' => 'bar',3,60));
I want to create facebook like buttons based on the beautified names. In order to do that I need the $beautiful_urls variable I use in the routes.php in the Foo controller.
How can I reach a variable in routes.php from a controller?
So far I tried to link it with App::use('routes','Config'); but it's not working. I also thought about sending the values as action parameters, but that doesn't seem like good practice... I know it's not a great idea to mix the config file with a controller's logic but I don't have any better idea so far.
I'm not cakephp user but simple search shows that there is class called ClassRegistry.
You can create class BeautifulUrls and store it there. According to docs it's singleton and It can be accessed from everywhere.
Also you can make BeautifulUrls implement ArrayAccess interface so you don't have to change your routes
I don't know if it's a good practice or not but my solution was to use the Configure class of CakePHP. It was straightforward to use and accessible everywhere in the code and the config files.
You can save key-value pairs with
Configure::write('key','value');
and read it again with
Configure::read('key');
This should be simple, and I've been searching all over Google, but I keep coming up with 'route' related advice.
I just want to perform a redirect to the same page and modify one of the query string parameters (either to clear one or set one).
I can't see how to do this anywhere.
An option could be to completely generate the URL manually and use this I guess, but that doesn't seem a very good method:
$this->router->generate("http://domain.com?a=1")
I hope I understand what you intend to do... In your controller (?) use
$this->generateUrl(
$request->attributes->get('_route'),
array_merge(
$request->query->all(),
array('param' => 'val') // change the param
)
);
to generate the url.
What is the reason of this redirect? I suppose that you wanna redirect from controller, don't you? I'm not sure what result you wanna achive. You have to be careful with redirecting in controller for same action controller (redirect loop).
However, in controller you can do that by:
public function indexAction()
{
// ...
return $this->redirect($this->generateUrl($request->attributes->get('_route'), array('paramName' => $paramValue)));
}
In my opinion, you should consider writing an event listener: http://symfony.com/doc/current/book/internals.html#handling-requests
It would be nice to be able to format a url in a portable way inside of a controller, for example for a JSON response. Is there an easy way to do this without creating an instance of Zend_View first?
Several thoughts here:
Even if you are generating a JSON response, you can still use the view object and view-scripts via the ContextSwitch and AjaxContext action helpers.
Even if you don't use a view-script for your response, you have probably already instantiated the view back at Bootstrap. So in the controller you wouldn't actually be creating the view as much as accessing it. So no additional overhead there.
If by "portable" you mean "cross-project", then maybe an action-helper? Drop it into another project, configure helper paths, and you're good to go. If by "portable" you mean "more aware of your application's routing", then you're probably stuck using the view object.
If saying "format URL" you meaning create an url from params, url view helper is you answer. You use it in controller the same way as in view and don't need to create new Zend_View instance - if you're using view renderer you have your helper in $this->view. So
//in view
$this->url(array('controller' => 'index', 'action' => 'default'));
//in controller
$this->view->url(array('controller' => 'index', 'action' => 'default'));
But if you look into code of url view helper, you'll see, that it's using router object to assemble routes/url/. So all you need is router object, which you can obtain in several ways, some of them:
//in controller
$router = $this->getFrontController()->getRouter();
//anywhere
$router = Zend_Controller_Front::getInstance()->getRouter();
//and then
$router->assemble(array('controller' => 'index', 'action' => 'default'));
You can also use HelperBroker, retrieve viewRenderer from there, retrieve a view and run helper method.
I have a controller that is called with AJAX (sends JSON data), so I don't use a view.
I need to use a personnal view helper to format my data, but in my controller.
Is that possible ?
Or maybe I am doing it wrong (maybe I should have a view, but how with JSON) ?
You can access any ViewHelper from the Controller by
$this->view->helpername(/*params*/);
// or
$helper = $this->view->getHelper('helpername');
// or
$broker = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$broker->getView()->helpername(/*params*/);
See Zend: How to use a custom function from a view helper in the controller?
However, you might be right that you are doing it wrong (funny pic btw), but I cannot really tell from your question. Please refine it as to why you need to call the view helper and what it is supposed to format.
Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');
Just be sure that the returned view is the view you want. Because down the line, the view may get overwritten and on the controller you have a spank new view.
And all those values you setup on the view on the action helper and the like... before the controller is kicked in? All gone with the wind!
So test before assuming that if you get a view resource. it is really the same view resource you expect, and that all your vars are still there.
You may be surprised as i was!
You can create an instance of a Helper .. this will work in Controllers, Models and everywhere you need the Helper.
eg.
// create Instance
$serverUrl_helper = new Zend_View_Helper_ServerUrl();
// get the ServerUrl
$serverUrl = $serverUrl_helper->serverUrl();
Another approach is to use the ContextSwitch or AjaxContext action-helpers. This allows you to use a view-script from which you can then call your view-helper in the standard manner.
Just use action helpers, many of view helpers are available as action helpers too.
Or directly by using Zend_Date or sprintf.