7

We can create php scripts which runs outside magento. And also use magento functionalities by using the following code snippet in it.

define('ROOT', '');
    $mage_php_url = ROOT.'app/Mage.php';

    if (!empty($mage_php_url) && file_exists($mage_php_url) && !is_dir($mage_php_url))
    {
        // Include Magento's Mage.php file.
        require_once ( $mage_php_url );
        umask(0);
        Mage::app();
    }

In these type of scripts we can directly call models, blocks, helpers etc. But how can we call to a controller action in here ? Any suggestions will be appreciated.

  • why would you need this and not do a request to that controller/action directly? – FlorinelChis Sep 30 '13 at 16:11
24

Magento isn't really designed to do this, which means you can use controller objects as you would any other object, but there will be complications. Since these methods are meant to be called from an HTTP context, they'll very often do things with the request and response objects, or rely on some bit of session state that just doesn't exist when you're running things from the command line.

That said, the following will work

// require you file        
if(!class_exists('Mage_Customer_AccountController')) //in case the class already exists
{
    require_once('Mage/Customer/controllers/AccountController.php');        
}

// instantiate your controller, using the `Mage:app()` object to grab the required request and response
$controller         = new Mage_Customer_AccountController(
                            Mage::app()->getRequest(),
                            Mage::app()->getResponse()
                        );        

// grab request and response object to manipulate as needed 
// (i.e. controller action expects post variables, etc.)

$request            = $controller->getRequest();
$response           = $controller->getResponse();

//manipulate things as per above

//call the action
$controller->someAction();
  • Thank you very much. I tested the code. It worked perfectly. – Sukeshini Oct 1 '13 at 3:48
  • How to pass Param in non parameterised action of core controller with$controller->someAction(); as in my case i need to remove wishlist item from index controller's removeAction() – abhishek Dec 16 '16 at 7:18
  • Pls let me know how to use in magento 2? I'm try using with include controller class into __construct function but it not pass must be an instance. – xanka Oct 6 '17 at 7:21
1

Here is a way to do it:

file_get_contents('http://url.com/[...]/controller/action/');

Done! :)

Or you can override the request (so it seem like the request is to that action) and then just call Mage::run().

On a more serious note, depends a lot on what you are actually doing in that action. Do you need Magento instantiated, do you need the entire request flow and so on.

If you don't need the request flow and so on, you can do:

Mage::app();
require_once 'path/to/controller.php';
$my_controller = new YourController;
$my_controller->doSomething();

If you need more the you can start digging by looking at Mage::run() function. Another good starting point is api.php, where you can see how to instantiate a lightweight version of Magento. Then just follow the request flow.

Looking at Alan's comment: If your controller is an instance of Mage_Core_Controller_Varien_Action (and your Magento controllers should be), you need to send a request object and a response object to the contructor:

 public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array())
    {
        $this->_request = $request;
        $this->_response= $response;

        Mage::app()->getFrontController()->setAction($this);

        $this->_construct();
    }
  • 4
    Controller objects require a request and response object in their constructor. – Alan Storm Sep 30 '13 at 19:33
  • 2
    two not recommended ways at once :-( requesting data by calling own uri and doing this with fopen & co – bukart Jul 1 '14 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.