requiring a file from laravel facade file - php

I have a facade 'CustomMage', but from which I'm not able to include magento api.
<?php namespace Bond\CustomMage;
class CustomMage {
/* Using mage configurations*/
public static function changeIsActive($table, $id, $status){
$api_path = \Config::get('api.mage_soap_api_path');
require_once("{$api_path}app/Mage.php");
/*umask(0);
Mage::app('default');
$quote = Mage::getModel('sales/quote')->load($id);
$quote->setIsActive($status)->save();*/
}
}
I'm getting this error,
[2018-02-07 08:47:09] local.ERROR: exception 'ErrorException' with
message 'include(Bond\CustomMage\Mage.php): failed to open stream: No
such file or directory' in
/var/www/html/FootballTicketPad/ecomm/lib/Varien/Autoload.php:93
But this code is ok if i use it in app/helpers.php.
function GetModal($table, $id){
$api_path = \Config::get('api.mage_soap_api_path');
require_once("{$api_path}app/Mage.php");
umask(0);
Mage::app('default');
return Mage::getModel($table)->load($id);
}

Related

Can't instantiate a class inside another PHP

How can I instatiate athe class OM inside the MySQL_DataMapper?
Here is the MySQL_DataMapper.php:
<?php
namespace asc {
include('../../models/OM.php');
class MySQL_DataMapper
{
[omitted code for simplicity]
public function fetchAllOMs()
{
$query = "REALLY LONG QUERY";
$result = $this->pdo->query($query);
$OMs = array();
while ($row = $result->fetch()){
$OM = new OM($row['id_organizacao_militar'], $row['nome'], $row['sigla'], $row['forca_armada']);
array_push($OMs, );
}
var_dump($OMs);
}
}
}
Here is the error I get:
Warning: include(../../models/OM.php): failed to open stream: No such file or directory in /home/alekrabbe/PhpstormProjects/stm_asc/controller/database/MySQL_DataMapper.php on line 10
Warning: include(): Failed opening '../../models/OM.php' for inclusion (include_path='.:/usr/share/php') in /home/alekrabbe/PhpstormProjects/stm_asc/controller/database/MySQL_DataMapper.php on line 10
Fatal error: Uncaught Error: Class 'asc\OM' not found in /home/alekrabbe/PhpstormProjects/stm_asc/controller/database/MySQL_DataMapper.php:37 Stack trace: #0 /home/alekrabbe/PhpstormProjects/stm_asc/views/cadastro-militar.php(13): asc\MySQL_DataMapper->fetchAllOMs() #1 {main} thrown in /home/alekrabbe/PhpstormProjects/stm_asc/controller/database/MySQL_DataMapper.php on line 37
I don't understand why it fails as I did just that on another php file and it worked there. Thank you.
EDIT 1
Forgot to mention but the class OM.php is also in the asc namespace.
Here it is:
<?php
namespace asc{
class OM
{
private $id;
private $nome;
private $sigla;
private $forca_armada;
public function __construct($id, $nome, $sigla, $forca_armada)
{
$this->id = $id;
$this->nome = $nome;
$this->sigla = $sigla;
$this->forca_armada = $forca_armada;
}
[Gets and Sets]
}
}
You're inside a namespace:
namespace asc {
So references to OM become asc\OM. Fix this by anchoring to the root namespace:
$OM = new \OM(...);
Try include __DIR__ . "/../../models/OM.php"; and make sure the path is correct
EDIT: the better way is to use an autoloader as #Alex Howansky mentioned in a comment on his answer, but in a pinch just make sure the path is correct and use DIR.

Lumen 5.6 Error handling (throws exception twice)

This is my code to handle any error:
App\Exceptions\Handler::class
public function render($request, Exception $e)
{
$fe = \Symfony\Component\Debug\Exception\FlattenException::create($e);
$statusCode = $fe->getStatusCode();
$code = $fe->getCode();
$message = $fe->getMessage();
$errorObj = new \App\LOHDomain\Entities\Error($code, $message);
return response()->json(['data' => null, 'error' => $errorObj], $statusCode);
}
when I parse a fake WSDL URL to the SoapClient it throws two exceptions
{"data":null,"error":{"code":"0","message":"SOAP-ERROR: Parsing WSDL: Couldn't load from 'asdsd' : failed to load external entity \"asdsd\"\n"}}
{"data":null,"error":{"code":"1","message":"SOAP-ERROR: Parsing WSDL: Couldn't load from 'asdsd' : failed to load external entity \"asdsd\"\n"}}
So the json response became invalid
When commenting these line of codes in the vendor, it throws one exception:
Laravel\Lumen\Concerns\RegistersExceptionHandlers trait
protected function registerErrorHandling()
{
error_reporting(-1);
set_error_handler(function ($level, $message, $file = '', $line = 0) {
if (error_reporting() & $level) {
throw new ErrorException($message, 0, $level, $file, $line);
}
});
set_exception_handler(function ($e) {
$this->handleUncaughtException($e);
});
// register_shutdown_function(function () {
// $this->handleShutdown();
// });
}
So, What is the problem? and how to solve it without editing in the vendor?
The solution is to clear the last error, because it fired twice.
The error exception.
The second is the shutdown function.
So, the solution is:
App\Exceptions\Handler::class
public function render($request, Exception $e)
{
$fe = \Symfony\Component\Debug\Exception\FlattenException::create($e);
$statusCode = $fe->getStatusCode();
$code = $fe->getCode();
$message = $fe->getMessage();
$errorObj = new \App\Domain\Entities\ResponseEntites\Error($code, $message);
/**
* This line of code resolves the issue
*
* To reproduce the issue :
* 1) Comment this following line of code
* 2) Provide a fake WSDL URL to the SoapClient
*
* Recommendation: Remove this line if you aren't using the SoapClient
*/
error_clear_last();
return new \Illuminate\Http\JsonResponse(['data' => null, 'error' => $errorObj], $statusCode);
}
This isn't the best solution (but this is the best solution, that I tried in my case).
If you have a better tested solution please share it.
links:
Fatal exceptions are handled twice
Code change

run script in background(shell_exec) fails on composer/PHP-DI function

I run that script manually and everything works,
$container = require '../config/bootstrap.php';
try {
$test = $container->get(Test::class);
$test->run();
}
catch(\Exception $e) {
echo $e->getMessage() . "\n";
die;
}
but when I call that file from another folder like the following :
$script = __DIR__ .'/folder/test-service.php';
$output = shell_exec('php '.$script);
var_dump($output);
I get the following error:
Fatal error: require(): Failed opening required '../config/bootstrap.php' (include_path='.:') in /anotherFolder/folder/test-service.php on line 13
and also after that, I got a lot of error that files do not exist.
bootstrap.php include the class mapping of PHP-DI
require '../vendor/autoload.php';
use DI\ContainerBuilder;
$containerBuilder = new ContainerBuilder;
$containerBuilder->addDefinitions(__DIR__ . '/config.php');
$container = $containerBuilder->build();
return $container;
The require instruction is relative to your current directory.
You could replace the line with require __DIR__.'/../config/bootstrap.php';

Autoload error in php

I am trying autoload function in php. The files are all in the same directory.
I have a file called aviation.php with the following code:
class Avaitor{
public function __construct(){
echo "Fly high<br>";
}
}
Then in my autoloader file I am trying to do this:
function __autoload($fileName){
if(file_exists($fileName . ".php"))
require_once $fileName . ".php";
}
//require_once "aviatior.php";
$pilot = new Avaitor();
But I am getting this error:
Fatal error: Uncaught Error: Class 'Avaitor' not found in
/Applications/MAMP/htdocs/php_oop/autoload.php:22 Stack trace: #0
{main} thrown in /Applications/MAMP/htdocs/php_oop/autoload.php on
line 22
Just to test that require_once does find my aviator.php I tried it out and then commented it out.
What am I doing wrong here?

How to import data from excel to mysql database using php

I am using PHPExcel to import a XLSX file to my related database. But while running the function I am getting the error. My code looks like shown below.
Controller:
<?php if (!defined ('BASEPATH')) exit ('No direct access allowed');
class ExcelController extends CI_Controller
{
public function index()
{
//load library excel
$this->load->library('excel');
//Here i used microsoft excel 2007
$objReader= PHPExcel_IOFactory::createReader('Excel2007');
//Set to read only
$objReader->setReadDataOnly(true);
//Load excel file
$objPHPExcel=$objReader->load('data.xls'); // error in this line
$objWorksheet=$objPHPExcel->setActiveSheetIndex(0);
//load model
$this->load->model('user_model');
//loop from first data untill last data
for($i=2;$i<=77;$i++)
{
$name= $objWorksheet->getCellByColumnAndRow(0,$i)->getValue();
$address= $objWorksheet->getCellByColumnAndRow(1,$i)->getValue();
$data_user=array('name'=>$name, 'username'=>$address);
$this->user_model->add_data($data_user);
}
}
}
?>
model:
<?php
if (!defined ('BASEPATH')) exit ('No direct access allowed');
class User_model extends CI_Controller
{
public function __construct() {
parent::__construct();
}
public function add_data($data_user)
{
$this->load->database();
$this->db->insert('data',$data_user);
return $this->db->insert_id();
}
}
?>
Error in my code:
Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open data.xls for reading! File does not exist.' in C:\xampp\htdocs\ci_excel\application\third_party\PHPExcel\Reader\Excel2007.php:347 Stack trace: #0 C:\xampp\htdocs\ci_excel\application\controllers\excelcontroller.php(19): PHPExcel_Reader_Excel2007->load('data.xls') #1 [internal function]: ExcelController->index() #2 C:\xampp\htdocs\ci_excel\system\core\CodeIgniter.php(359): call_user_func_array(Array, Array) #3 C:\xampp\htdocs\ci_excel\index.php(202): require_once('C:\xampp\htdocs...') #4 {main} thrown in C:\xampp\htdocs\ci_excel\application\third_party\PHPExcel\Reader\Excel2007.php on line 347
Judging by the error message and your comment, it looks like you are using an incorrect filepath.
$objPHPExcel=$objReader->load('data.xls');
In CodeIgniter paths are relative to the entry script, usually index.php.
Use a relative file path to this location or alternatively an absolute path.
I think you used incorrect path , change this
$objPHPExcel=$objReader->load('data.xls'); // error in this line
To
$objPHPExcel=$objReader->load('./application/third_party/Excel5/data.xlsx');

Categories