0

I want to create a custom script for this I have created folder test on root

and two files inside folder index.php , test.php

My index.php Looks like

require DIR . '../../app/bootstrap.php';

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

$app = $bootstrap->createApplication('Test');

$bootstrap->run($app);

and Test.php looks like below

class Test extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface
{
    public function launch()
    {
        echo "launched";
        return $this->_response;

    }

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

}

But am getting 404 error example.com/test

example.com/test/index.php getting File not found

3 Answers 3

0

Execute Your Custom Script Like This:-

In Browser :-

localhost/yoursite/test/filename.php

Using Command :-

php -f test/yourfilename.php
0

Use below code , its a working copy

Move both the files inside test folder in root directory

test/test.php

<?php    
 require __DIR__ . '../../app/bootstrap.php';
 require dirname(__FILE__) . '/customScript.php';

 $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

 $app = $bootstrap->createApplication('customScript');

$bootstrap->run($app);

?>

then test/customScript.php

 <?php

   class customScript extends \Magento\Framework\App\Http implements 

 \Magento\Framework\AppInterface {

 public function launch()
{
    $this->_state->setAreaCode('frontend'); //Set area code 'frontend' or 'adminhtml
     $id = 65;//load any product id
     $_product = $this->_objectManager->create('\Magento\Catalog\Model\Product')->load($id);

      echo $_product->getName();

    return $this->_response;
}

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


}

then hit url in browser like below

http://your_site/test/test.php

0

After testing, you're wrong here:

require DIR . '../../app/bootstrap.php';

Need to change to

require __DIR__ . '/../app/bootstrap.php';

See in pub/index.php as a good example.

Full code

test/index.php

<?php
require __DIR__ . '/../app/bootstrap.php';

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

$app = $bootstrap->createApplication('Test');

$bootstrap->run($app);

test/Test.php

<?php

class Test extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface
{
    public function launch()
    {
        echo "launched";
        return $this->_response;

    }

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

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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