最新消息:觉得本站不错的话 记得收藏哦 博客内某些功能仅供测试 讨论群:135931704 快养不起小站了 各位有闲钱就打赏下把 My Email weicots#gmail.com Please replace # with @

我知道magento1x 可以这样从外部引用其内部函数 但Magento 2x 怎么弄 ?

14987浏览
open
发布

在Magento1,我可以创造一个加载? 我只需要实例化Mage_Core_Model_App 类文件,然后我可以加我的外部引用的代码,用于测试目的。
类似于这样的

<?php
//一些设置
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);
//实例化引用代码
Mage::app();
//这里是我的代码

然后我可以 通过浏览器看到并测试我的文件
我如何才能像magento 1X 在magento 2X 中也这么做

转载请注明:(●--●) Hello.My Weicot » 我知道magento1x 可以这样从外部引用其内部函数 但Magento 2x 怎么弄 ?

  1. ajiang-tuzi 管理员 2016年3月17日
    你可以看看magento 2 中core是怎么写的
  2. 好主意, but...不;过 从core.php: 的代码 $app = $bootstrap->createApplication('Magento\Framework\App\Cron', ['parameters' => ['group::']]);.来看 我应该自己创建应用模块?

4 答案

发布
管理员 2016年3月17日

我从网上给你找了一段 你可以看看
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.

<?php
require __DIR__ . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('TestApp');
$bootstrap->run($app);

Then I created a file called TestApp.php in the same place with this content.

<?php
class TestApp
    extends \Magento\Framework\App\Http
    implements \Magento\Framework\AppInterface {
    public function launch()
    {
        //dirty code goes here. 
        //the example below just prints a class name
        echo get_class($this->_objectManager->create('\Magento\Catalog\Model\Category'));
        //the method must end with this line
        return $this->_response;
    }

    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        return false;
    }

}

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 method createApplication 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 method catchException return false 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. This run 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:

$response = $application->launch();

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 the launch 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 the launch 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.

本答案来自@Marius

转载请注明:(●--●) Hello.My Weicot » 回答我知道magento1x 可以这样从外部引用其内部函数 但Magento 2x 怎么弄 ?

#
发布
管理员 2016年3月17日

我是这样搞得

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/../app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$quote = $obj->get('Magento\Checkout\Model\Session')->getQuote()->load(1);
print_r($quote->getOrigData());

转载请注明:(●--●) Hello.My Weicot » 回答我知道magento1x 可以这样从外部引用其内部函数 但Magento 2x 怎么弄 ?

#1
发布

这里还有个解释
The Important part is the \Magento\Framework\App\Bootstrap::create
but as the Bootstrap::init() method is private, and there happens a lot of important stuff, public methods calling it are needed.
Thats on the one side the createApplication() and following the run() method, but also the getDirList() and getObjectManager() method, which both need no argument.
So an Application is not needed, the downsides are, that the error Handler is not initialized.
@Manashvi birla

转载请注明:(●--●) Hello.My Weicot » 回答我知道magento1x 可以这样从外部引用其内部函数 但Magento 2x 怎么弄 ?

#2
发布
管理员 2016年3月17日

Based on @Marius’s answer I came up with this.

It’s works via both the command line as well as the browser, which I find useful.

Here’s a sample script to programatically delete category.

scripts/abstract.php
<?php
use \Magento\Framework\AppInterface as AppInterface;
use \Magento\Framework\App\Http as Http;

use Magento\Framework\ObjectManager\ConfigLoaderInterface;
use Magento\Framework\App\Request\Http as RequestHttp;
use Magento\Framework\App\Response\Http as ResponseHttp;
use Magento\Framework\Event;
use Magento\Framework\Filesystem;
use Magento\Framework\App\AreaList as AreaList;
use Magento\Framework\App\State as State;

abstract class AbstractApp implements AppInterface
{
    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager,
        Event\Manager $eventManager,
        AreaList $areaList,
        RequestHttp $request,
        ResponseHttp $response,
        ConfigLoaderInterface $configLoader,
        State $state,
        Filesystem $filesystem,
        \Magento\Framework\Registry $registry
    ) {
        $this->_objectManager = $objectManager;
        $this->_eventManager = $eventManager;
        $this->_areaList = $areaList;
        $this->_request = $request;
        $this->_response = $response;
        $this->_configLoader = $configLoader;
        $this->_state = $state;
        $this->_filesystem = $filesystem;
        $this->registry = $registry;
    }

    public function launch()
    {
        $this->run();
        return $this->_response;
    }

    abstract public function run();

    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        return false;
    }
}

scripts/delete-category.php

<?php
require dirname(__FILE__) . '/../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
require dirname(__FILE__) . '/abstract.php';

class CreateCategoriesApp extends AbstractApp
{

    public function run()
    {
        $this->_objectManager->get('Magento\Framework\Registry')
            ->register('isSecureArea', true);

        $category = $this->_objectManager->create('\Magento\Catalog\Model\Category');
        $category = $category->load(343);

        $category->delete();
    }
}

/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('CreateCategoriesApp');
$bootstrap->run($app);

Then I just run it like php scripts/delete-category.php
@Luke Rodgers

转载请注明:(●--●) Hello.My Weicot » 回答我知道magento1x 可以这样从外部引用其内部函数 但Magento 2x 怎么弄 ?

#3

请登录或者 注册 来提交答案

与本文相关的文章

  • 暂无相关文章!
蜀ICP备15020253号-1