In magento 1 I could create a file where I only needed to instantiate the Mage_Core_Model_App
class and then I could add my "dirty" code for test purposes.
Something like this test.php
:
<?php
//some settings
error_reporting(E_ALL | E_STRICT);
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
//instantiate the app model
Mage::app();
//my toy code in here.
Then I was able to call test.php
in the browser and see what I'm doing.
How can I do the same for Magento 2?
Best Answer
Based on @Flyingmana's answer I did a little digging and come up with a solution. It seams to work for me.
First my solution, then some explanations.
I've created a file called
test.php
in the root of my magento instance.Then I created a file called
TestApp.php
in the same place with this content.Now I can just call
test.php
in the browser and everything that is placed in TestApp::launch() will be executed.Now, why this works:
The method
createApplication
from the bootstrap class is the most important part. It creates an instance of an application class. The methodcreateApplication
expects an implementation of the\Magento\Framework\AppInterface
that contains 2 methods.So I created my own class in
TestApp
that implements that interface. I made the methodcatchException
returnfalse
always because I don't want my app to handle exceptions. In case something is wrong, just print it on the screen.Then I implemented the method
launch
. this one is called by\Magento\Framework\App\Bootstrap::run
. Thisrun
method does almost the same thing no matter what the application passed as a parameter is.The only thing that depends on the application is this line:
This means that calling
\Magento\Framework\App\Bootstrap::run
will init the Magento env (maybe do some other crazy stuff...I haven't checked everything yet) then calls thelaunch
method from the application.That's why you need to put all your dirty code inside that method.
Then the
\Magento\Framework\App\Bootstrap::run
calls$response->sendResponse();
where$response
is what thelaunch
method returns.That's why
return $this->_response;
is needed. It just returns an empty response.I made my app class extend
\Magento\Framework\App\Http
so I will already have request and response parameters (and others), but you can make your class extend nothing. Then you need to copy the constructor from the\Magento\Framework\App\Http
class. Maybe add more parameters in the constructor if you need it.