I have a one controller SearchController in Hotel module and i have a one method Search in this controller like below.
public function searchAction()
{
// Code for search
// want to call getlatlng method
}
now i created a one new controller DistanceController in same module and
created a one getlatlng method in distance controller.my getlatlng method like this.
public function getlatlng()
{
$location = $_REQUEST['lat'].','.$_REQUEST['lng'];
return $location;
}
Now i want to call a getlatlng method in searchAction method. which returns a latitude and longitude of the current place.I pass latitude and logitude to getlatlng function using post or get.
So how can i call getlatlng method in searchAction method?
You can call like this, If you are using ZF Version 1.x:
class SearchController extends Zend_Controller_Action
{
public function searchAction() {
echo "search_action_from_SearchController";
require_once ('DistanceController.php');
$distanceCtrl = new DistanceController($this->_request, $this->_response);
$distanceCtrl->xyzAction();
die;
}
}
class DistanceController extends Zend_Controller_Action
{
public function getlatlng() {
echo "getlatlng_from_DistanceController";
die;
}
}
Output:
URL: http://www.example.com/search/search
search_action_from_SearchController
getlatlng_from_DistanceController
Related
To stay basic I would like to create a bookmark app
I have a simple bookmarklet
javascript:location.href='http://zas.dev/add?url='+encodeURIComponent(location.href)
I created a rest controller
<?php
use zas\Repositories\DbLinkRepository;
class LinksController extends BaseController {
protected $link;
function __construct(DbLinkRepository $link) {
$this->link=$link;
// ...
//$this->beforeFilter('auth.basic', array('except' => array('index', 'show', 'store')));
// ...
}
public function index()
{
//return Redirect::to('home');
}
public function create()
{
}
public function store()
{
return 'hello';
//$this->link->addLink(Input::get('url'));
//return Redirect::to(Input::get('url'));
}
public function show($id)
{
//$url = $this->link->getUrl($id);
//return Redirect::to($url);
}
public function edit($id)
{
}
public function update($id){
}
public function destroy($id){
}
}
in the routes.php, I created a ressource
Route::resource('links','LinksController');
and as I want to redirect /add to the store method I added
Route::get('/add',function(){
return Redirect::action('LinksController#store');
});
but it never display the hello message, in place it redirects me to
http://zas.dev/links
I also tried with
return Redirect::route('links.store');
without much success
thanks for your help
Ok I now get what you are trying to do. This will work:
Route::get('add', 'LinksController#store');
Remove:
Route::resource('links','LinksController');
and remove:
Route::get('/add',function(){
return Redirect::action('LinksController#store');
});
Sorry it took so long!
The problem is that once you Redirect::, you loose all the Input values, so you should manually give them to your controller when you do the redirect, like so :
Redirect::route('links.store', ["url" => Input::get("url")]);
Finally add an $url parameter to your store method to receive the value we give it in the previous method, like this :
public function store($url) {
$this->link->addLink($url);
return Redirect::to($url);
}
In my RestController which extends AbstractRestfulController, I can get the route params in the implemented functions such as...
public function create($data)
{
$entity = $this->params()->fromRoute('entity');
}
... but when I do the same in the constructor like this
public function __construct()
{
$entity = $this->params()->fromRoute('entity');
}
I get Call to a member function getParam() on a non-object.
Why is that? How can I get the route parameters in the constructor?
What I am trying to do
Since I'm trying to create a generic controller, there is a part of the restful route that is shared for all actions (resp. verbs). The entity for which the request is made. I'd like to store this in a class parameter for convenience.
Normally you'd write a method to proxy to whatever value you need, and just call that method, it's only a little more expensive to call $this->getEntity() than it is to call $this->entity, which, as far as I can tell is the stated aim
class RestController
{
protected $entity;
public function getEntity()
{
if (!$this->entity) {
$this->entity = $this->params()->fromRoute('entity');
}
return $this->entity;
}
}
If you really do want to pre-populate the entity property, the simplest method is to use an initializer, and move the code from your __constructor to init(). Have your controller implement \Zend\Stdlib\InitializableInterface
use Zend\Stdlib\InitializableInterface;
class RestController extends AbstractRestfulController implements InitializableInterface
{
protected $entity;
public function init() {
$this->entity = $this->params()->fromRoute('entity');
}
}
Add an initializer to the controller loader in your module boostrap
use Zend\Stdlib\InitializableInterface;
class Module
{
public function onBootstrap(MvcEvent $e)
$sm = $e->getApplication()->getServiceManager();
$controllers = $sm->get('ControllerLoader');
$controllers->addInitializer(function($controller, $cl) {
if ($controller instanceof InitializableInterface) {
$controller->init();
}
}, false); // false tells the loader to run this initializer after all others
}
}
That would not make any sense as the route is matched to a particular action.
You can't route to a constructor, therefore how could you get route parameters there?
If you give an idea of what you are trying to do then I could suggest a better/nicer way to do it
How do you unregister or disable a controller plugin from within a controller action.
class IndexController
{
public function indexAction()
{
// disable plugin here
// do some other stuff
}
}
First get an instance of the front controller, then an instance of the plugin by a given name (class name), and lastly call the unregisterPlugin() method.
public function disableSomePluign()
{
$frontController = $this->getFrontController();
$plugin = $frontController->getPlugin('Some_Plugin');
$frontController->unregisterPlugin($plugin);
}
in order to avoid copy/paste, i can use a unique view for different actions, i can do this using one of these ways
$this->renderScript('index/index.phtml');
$this->view->var = "hello world";
or
$this->_helper->viewRenderer('index');
$this->view->var = "hello world";
if i want to use a different controller i have to use the 1st one, the viewRenderer is ok, but when i use the renderScript it shows nothing like var is not defined. how can i assign values to the view script?
index.phtml would be somthing like this
echo $this->var ;
It should work the way you described it
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
$this->view->var = 'echo me in any action viewscript and i will show you text';
}
public function indexAction()
{
// action body
$this->view->test = 'Don\'t put me here becuase this is not the action that is run';
}
public function testAction()
{
// action body
$this->view->test = 'Hello world';
$this->renderScript('index/index.phtml');
// or $this->_helper->viewRenderer('index');
}
}
in my view (index.phtml)
i have
<?php echo $this->test;?>
When i go to /index/test/ it will show "hello world"...
Also when i do it in another controller it gives me the result
class MyController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// action body
$this->view->test = 'Hello world';
$this->renderScript('index/index.phtml');
}
}
How do you specify a custom view script for a given Controller Action method?
For example:
Class UserGalleryController extends Zend_Controller_Action
{
public function fooAction()
{
$this->view->actionMsg = 'foo';
// (uses foo.phtml automagically)
}
public function barAction()
{
$this->view->actionMsg = 'bar';
//use foo's view script ?????
}
}
I basically want to have one view script (foo.phtml)
Thanks :-)
public function barAction()
{
$this->view->actionMsg = 'bar';
$this->render('foo');
}