3

In magento 1 if i use the code below file (ex Product) in model directory

<?php

class Crowderia_Demo_Model_Product {

    public function say() {
        echo "Testing";
    }
}

If i use test.php file in project directory I can get the method using mage.php file like below

<?php

require_once 'app/Mage.php';

Mage::app();

$product = new Product;
$product->say();

How can I do this in magento 2

Please help me I'm new to magento

5
<?php
use \Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

try {
      $objectManager->create('Crowderia\Demo\Model\Product');
      $objectManager->say();
} catch (\Exception $e) {
    echo $e->getMessage();       
}

------ I have tested below code and it is working fine for me. Please try to update it by your own module. ------

<?php
use Magento\Framework\App\Bootstrap;

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

$params = $_SERVER;

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

$obj = $bootstrap->getObjectManager();

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

$quote = $obj->get('Magento\Catalog\Model\Product');   
//I have created say fucntion under Product.php file
echo '<pre>';
print_r($quote->say());
echo '</pre>';
  • Parse error: syntax error, unexpected ''Crowderia\Demo\Model\Product)' (T_ENCAPSED_AND_WHITESPACE) in C:\xampp\htdocs\magento\test.php on line 14 – Hemal Oct 5 '17 at 9:28
  • @Hemal Please see updated answer. – Vishwas Soni Oct 5 '17 at 9:30
  • Fatal error: Cannot declare class Product, because the name is already in use in C:\xampp\htdocs\magento\app\code\Crowderia\Demo\Model\Product.php on line 3 – Hemal Oct 5 '17 at 9:37
  • Add namespace Crowderia\Demo\Model; in top of the file app\code\Crowderia\Demo\Model\Produc‌​t.php – Vishwas Soni Oct 5 '17 at 9:42
  • Thanks for the help now it says Fatal error: Uncaught Error: Call to undefined method Magento\Framework\App\ObjectManager::say() in C:\xampp\htdocs\magento\test.php:14 Stack trace: #0 {main} thrown in C:\xampp\htdocs\magento\test.php on line 14 – Hemal Oct 5 '17 at 9:44
2
<?php

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');
try {
    $obj->create('Crowderia\Demo\Model\Product);
} catch (\Exception $e) {
    while ($e) {
        echo $e->getMessage();       
    }
}
  • Parse error: syntax error, unexpected ''Crowderia\Demo\Model\Product)' (T_ENCAPSED_AND_WHITESPACE) in C:\xampp\htdocs\magento\test.php on line 14 – Hemal Oct 5 '17 at 9:30
1

You can use bootstrap.php here. refer below example:

use Magento\Framework\App\Bootstrap;

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

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

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Your Class');
  • How can i get the method – Hemal Oct 5 '17 at 9:33
1

Please create an external file in Magento root directory.

<?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    require 'app/bootstrap.php';

    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
    $app = $bootstrap->createApplication('Magento\Framework\App\Http');
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $objectManager->get('Magento\Framework\App\State')->setAreaCode('frontend'); // adminhtml

    // $bootstrap->run($app);

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.