I have a custom module with a controller action which performs a certain function,
class Company_CustomModule_ActionController extends Mage_Core_Controller_Front_Action
{
public function doAction()
{
// do something
}
}
I have another controller in the same module (lets say "test"). I would like to call an action within this "test" controller in the above mentioned controller and pass it a parameter like,
class Company_CustomModule_ActionController extends Mage_Core_Controller_Front_Action
{
public function doAction()
{
// do something
// call the index in the "Test" controller
Mage::app()->getFrontController()->getResponse($data)
->setRedirect(Mage::getUrl('company/test'))
->sendResponse();
}
}
My goal is to pass some data from the Action controller to the Test Controller and execute the index action in the Test Controller from within the Action Controller.
Note: in essence, I would like to pass on the POST data received by the Action Controller to the Test Controller.
How would I go about doing this? any guidance would he helpful.
I'm not overly familiar with Magento, but since it's built on the Zend Framework you should be able to use the forward helper and pass your data in the params parameter:
return $this->_forward('test', 'company', $module, $params);
Related
Is it possible to execute multiple functions in the same controller with one route. I thought it would be something like this but it doesn't work.
Route::get('getdata','controller#getData', 'controller#getData1', 'controller#getData2');
In the controller are these functions:
getData
getData1
getData2
Or is there a easier way?
In the controller
Add something like this.
class YourController extends Controller {
//...
protected function getAllData() {
//Executes the seperate functions.
$this->getData();
$this->getData1();
$this->getData2();
}
//...
}
This will execute the functions respectively.
Then from your route, you just call YourController#getAllData as the function of the controller.
It does not make sense if multiple controller actions are responsible for a single route. That's not how MVC works. You should have one and only one action for each route, and call every other function you need inside that action.
And remember, for best practices each method of the controllers must contain only the code to respond the request, not business logic, and if you have any other functions which needs to be called, put them in another other classes (layers).
class MyController extends Controller {
public function myAction(MyService $myService) {
$myService->getData();
// not $this->getData()
}
}
I'm completely confused on how Phalcon PHP renders its views. I want to create a new page called "manager".
From my understanding by creating a controller I can link it to a view. I create a controller called ManagerController.php and then added a view in views/manager/index.volt.
I added a bit of text the volt file to check if it works. When I go to /manager/ nothing shows up.
Am I doing this correct or do I have to assign a view somewhere?
class ManagerController extends ControllerBase
{
public function initialize()
{
$this->tag->setTitle('Files/My Files');
parent::initialize();
}
}
The initialize function on a controller is an event ran after constructing the controller
In order to display view for that controller it is necessary to at least setup an index action
In your you are interested in rendering a route of /manager/ , this will correspond to indexAction
class ManagerController extends ControllerBase
{
public function initialize()
{
$this->tag->setTitle('Files/My Files');
parent::initialize();
}
public function indexAction()
{
// This will now render the view file located inside of
// /views/manager/index.volt
// It is recommended to follow the automatic rendering scheme
// but in case you wanted to render a different view, you can use:
$this->view->pick('manager/index');
// http://docs.phalconphp.com/en/latest/reference/views.html#picking-views
}
// If however, you are looking to render the route /manager/new/
// you will create a corresponding action on the controller with RouteNameAction:
public function newAction()
{
//Renders the route /manager/new
//Automatically picks the view /views/manager/new.volt
}
}
Im comming from CodeIgniter.
There if you had a controller like this:
class Article extends CI_Controller{
public function comments()
{
echo 'Look at this!';
}
}
You could access the comments() function using the URL like this: example.com/Article/comments
How could I do something similar in Laravel?
The way I do it right now is specifiying a route like this:
Route::get('/Article/comments}', 'ArticleController#comments');
But I was hoping for a more dynamic way to do it as I don't want to keep on creating new routes for every function
The recommended way of dynamically calling controllers methods via URL, for Laravel users, is via RESTful Controllers:
<?php
class ArticleController extends controller {
public function getComment()
{
return 'This is only accesible via GET method';
}
public function postComment()
{
return 'This is only accesible via POST method';
}
}
And create your route using telling Laravel this is a RESTful Controller:
Route::controller('articles', 'ArticlesController');
Then if you follow
http://laravel.dev/articles/comments
Using your browser, you should receive:
This is only accesible via GET method
The way you name your controllers methods (getComment, postComment, deleteComment...) tells Laravel wich HTTP method should be used to call those methods.
Check the docs: http://laravel.com/docs/controllers#restful-controllers
But you can also make it dynamic using PHP:
class ArticlesController extends Controller {
public function comments()
{
return 'Look at this!';
}
public function execute($method)
{
return $this->{$method}();
}
}
Use a controller like this one:
Route::get('Article/{method}', 'ArticleController#execute');
Then you just have to
http://laravel.dev/Article/comments
I'll recommend that you stick with the laravel's way to create REST controllers, because that way you can have control over what HTTP Verb is being called with the controller method. The laravel way of doing this is just to add the HTTP Verb in front of the controller method, for your method comments if you want to specify a GET request in Laravel the name of the method would look like getComments.
For example, if you need to do a GET request for the article/comments URI, and then to create a new comment you want to use the same URI with another HTTP verb, lets say POST, you just need to do something like this:
class ArticleController extends BaseController{
// GET: article/comments
public function getComments()
{
echo 'Look at this!';
}
// POST: article/comments
public function postComments()
{
// Do Something
}
}
Further reading:
http://laravel.com/docs/controllers#restful-controllers
Now for your specific answer, this is the Laravel way of doing what you requested:
class ArticleController extends BaseController{
public function getComments()
{
echo 'Look at this!';
}
}
and in the routes.php file you'll need to add the controller as follows:
Route::controller('articles', 'ArticleController');
I'm little bit new to Zend, I want to use a controller action through out the entire application automatically, I don't have a clear idea to how to use it, thought about init() method, action helpers, etc.
Then instead of simply creating controller action create controllerAction Helper . Here you can find more about it
http://framework.zend.com/manual/en/zend.controller.actionhelpers.html
My_Helper_Magic extends Zend_Controller_Action_Helper_Abstract
{
public function preDispach()
{
//code inside here will run for entire application automatically
}
}
In your bootstrap do
Zend_Controller_Action_HelperBroker::addHelper(new My_Helper_Magic());
In Response to comment .
It depends upon your 'code fragment' , If your code fragment does not required to know nothing about module , controller, action , base url then you can use Bootstrap init function
like
public function _initAlways()
{
//see how this function name is prefixed with _init hence it will be called by ZF //everytime. You can put your code fragment here
//If your code fragment depends upon some stuff like baseurl then do action controller
// registration here instead
Zend_Controller_Action_HelperBroker::addHelper(new My_Helper_Magic());
}
In Response To comment
You can save any instance of your object inside Zend_Registy and retrieve it whereever you like
Inside Bootstrap.php
public function _initSetup()
{
$object = new My_Custom_Object();
Zend_Registry::set('my_custom_object',$object);
}
Later in your view or controller do
$myObject = Zend_Registry::get('my_custom_object'); //to access it
PROBLEM: I can't pass my variables to my custom layout from action helper's init method.
I have this action helper "My_Action_Helper_Initializer":
class My_Action_Helper_Initializer extends Zend_Controller_Action_Helper_Abstract
{
public function init()
{
$controller=$this->getActionController();
//variable passed to controller's view
$controller->view->flop="FLOOP!!";
//variable passed to controller
$controller->boom="BOOM!!";
}
}
In my controller "IndexController":
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
//print the variable passed from action helper
echo $this->boom;
}
}
then in my "layout.phtml":
//print variable passed from action helper
echo $this->flop;
So the "boom" variable echoed out by controller action is shown correctly.
The "flop" variable (passed to my layout) is not shown.
QUESTION: Why the variable passed to controller action is correctly output while the other one passed to the layout view is not?
Thanks
Luca
When your helper's init() called, ViewRenderer's init() wasn't yet. This is because of order in helpers stack.
If you enable strict standards error reporting, you should see something like this in your helper "Creating default object from empty value in ..."
You should consider moving your code to preDispatch() hook as init() method should be used for helper initialization.
To get view instance for controller:
function getView()
{
$controller = $this->getActionController();
if($view = $controller->view) {
return $view;
}
if($this->getFrontController()->getParam('noViewRenderer') {
return $controller->initView();//this view instance will not be used in Zend_Layout!
}
$vr = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
return $vr->initView();
}
if you want to pass parameter to layout, then use
$view = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->initView();
Action helpers are called so because they are a way to avoid code duplication in actions. Everything you do in an action helper is available to the actions but not to the view. That's the normal behaviour of actions, as long as you don't pass something to the view, the view doesn't know it. If you want to avoid code duplication in your views, create view helpers.
Sometimes it can make sense to create an action helper and a corresponding view helper.
--
preDispatch works() while init() doesn't because with init you're not actually hooking into the dispatch process.