This question already has an answer here:
Composer autoload file not working
(1 answer)
Closed 7 years ago.
I am trying to autoload a non class based file via composer
"autoload" : {
"files": ["app/routes.php"]
},
But i am unable to get the contents of this file in my scripts. I have included the vendor/autoload.php as well as ran dump-autoload.
What i want is if there is a way that we can see the list of files that are being autoloaded by composer in browser or in terminal so that i can be sure that the autoloading is working fine and there is some other problem in my code.
Thanks
Updated: File Heiarchy
Index.php
/**
* Including the Composer's autoloader
*/
require_once 'vendor/autoload.php';
/**
* Bootstrap our application
*/
require_once 'app/init.php';
Init.php
<?php var_dump($route); ?>
routes.php
<?php
$route = 'abc';
?>
So my problem is i want to access $route variable in my init.php file.
I've look in the autoload_real.php, and I've noticed that the files aren't included in the global scope.
public static function getLoader()
{
[...]
$includeFiles = require __DIR__ . '/autoload_files.php';
foreach ($includeFiles as $file) {
require $file;
}
[...]
}
See variable scope. Just add an echo "test"; in your routes.php file to confirm that it is properly included. Then you may want change routes.php with something like
function getRoutes()
{
return "abc";
}
and Init.php
<?php var_dump(getRoutes()); ?>
Did you include the autoload.php file generated by Composer?
require_once 'path/to/autoload.php';
Also, note that the file will be loaded on every request
Related
I am trying to use the invoice generator from webchemistry. I installed this with composer require webchemistry/invoice:^1.0.
I moved this folder to my APPPATH . 'third_party\vendor\'; folder. So in my config file I have the following line:
$config['composer_autoload'] = APPPATH . 'third_party\vendor\autoload.php';
my index.php file (in root) contains this code:
include_once BASEPATH.'../application/third_party/vendor/autoload.php';
In my controller I am trying to do the following :
public function createInvoice()
{
$company = new \WebChemistry\Invoice\Data\Company();
}
This results in the following error:
Message: Class 'WebChemistry\Invoice\Data\Company' not found
When in my IDE (PhpStorm) I ctrl + click on Company, it can resolve correctly and points to the correct file.
Why can PHP not resolve this to the correct file location?
add the file include and following code in your controller at the start
$config['composer_autoload'] = false; // no need change this, make it default
<?php
require_once(APPPATH . '/third_party/vendor/autoload.php');
use \WebChemistry\Invoice\Data\Company as Company;
class Invoice extends CI_Controller {
public function createInvoice(){
$company = new Company();
}
}
You should not do anything with vendor directory unless you know what you're doing. If you want to change directory where Composer installs dependencies, you can do this by setting vendor-dir in composer.json config:
{
...
"config":{
"vendor-dir": "third_party/vendor"
}
}
I'm working on a project and it's getting a little too hard for me...
I explain.
I need to parse PDF files with PHP, to analyse the content of those files. To do that, I use pdfparser.org library.
I firstly tried to include this library as usually, without any result.
After having read all the Internet, since this library requires Composer to be installed (and on my web hosting I can't get Composer installed), I have applied the Composer process on my Windows PC. I got the "vendor" folder with the "autoload.php" file. Fine !!
Then, I have tried to include it properly in CodeIgniter. The solution I chose is :
Creating a file "Pdfparser.php" in application/libraries/
class Pdfparser
{
public function __construct()
{
require_once APPPATH."/third_party/pdfparser.php";
}
}
Then, I add the PdfParser "Composer" application in application/third_party/, and in the /third_party/pdfparser.php I simply put :
if (!defined('pdfparser')) {
define('pdfparser', dirname(__FILE__) . '/');
require(pdfparser . 'pdfparser/autoload.php');
}
Then, I add this library to CodeIgniter /application/config/autoload.php as :
$autoload['libraries'] = array('pagination', 'form_validation','email','upload','pdfparser');
Finally, I call it in my function in application/controllers/Admin.php :
$parser = new Pdfparser();
$pdf = $parser->parseFile(myfile.pdf);
$full_text = $pdf->getText();
(This 4. block of code is directly taken from official Documentation here : http://www.pdfparser.org/documentation, and just adapted).
But now, I break the Internet... I have this error :
PHP Fatal error: Call to undefined method PdfParser::parseFile() in /path/application/controllers/Admin.php on line 3083
After having looked CodeIgniter documentation, I try to add the Composer autoloader to the core... in application/config/autoload.php I put :
$config['composer_autoload'] = APPPATH . "/third_party/pdfparser/autoload.php";
Of course, it doest not work. And I'm lost...
Use composer properly. $config['composer_autoload'] = TRUE; and inside your application folder run composer install smalot/pdfparser . Then inside your controller it should run, if not use Use :)
use Smalot\PdfParser;
class My_controller extends CI_Controller {
}
When using composer, to include a library in your project you do something like that :
composer install smalot/pdfparser
Then, to include the newly installed library, you only need to include the "autoload.php" file provided by composer :
<?php
include 'vendor/autoload.php';
$parser = new Pdfparser();
$pdf = $parser->parseFile(myfile.pdf);
$full_text = $pdf->getText();
var_dump($full_text);
Nothing more.
Replace your code
class Pdfparser
{
public function __construct()
{
require_once APPPATH."/third_party/pdfparser.php";
}
}
with
<?php
require_once APPPATH."/third_party/pdfparser.php";
class Pdfparser
{
public function __construct()
{
}
}
Include outside of your class.
Rather than using autoloading you can load library like this...
$this->load->library('library_name');
Example:
$this->load->library('pdfparser');
Routes.php
use MyMVC\Core\Route;
$route = new Route;
$route->add('/', 'HomeController#index');
$route->add('about', 'AboutController#index');
$route->add('contact', 'ContactController#index');
Index.php
<?php
/**
* Define Constants
*/
define('BASE_PATH', dirname(realpath(__FILE__)));
define('APP_PATH', BASE_PATH . "/app");
/**
* Including the Composer's autoloader
*/
require_once 'vendor/autoload.php';
/**
* Load the routes declarations
*/
require_once 'app/routes.php';
/**
* Bootstrap our application
*/
require_once 'app/init.php';
/**
* Initialize our beautiful framework
*/
$application = new \MyMVC\Application($route);
composer.json
"autoload" : {
"psr-4" : {
"MyMVC\\" : "app/"
},
"classmap": [
"app/Controllers",
"app/Helpers"
],
"files": ['app/routes.php'] // already removed this line
},
When using require_once it is giving undefined variable route error while if i use only require it shows the route object.
Why is that so ?
This kind of error only happens when the file is already loaded from other files. That's why it's not loading it again and you are not getting instance of $route object.
E.g,
Let's say
file1.php included routes.php
Now if you use:
require 'routes.php' (it will load same file again even already loaded)
requier_once 'routes.php' (it will not load the file if already loaded, and as you are not getting instance of $route variable, it's mean it's happening)
All symptoms suggest that 'app/routes.php' is included somewhere else before. Since it defines a variable, if such include does not happen in global scope the variable will be local to wherever it's called from.
Apart from using a dedicated debugger like Xdebug, you can use builtin tools to diagnose the issue. For instance, you have get_included_files() to get a list of included files in a given point. You can also add debug_print_backtrace() on top of 'app/routes.php' to find out where it's called from.
Note on updated question and follow-up comment: if you're trying to auto-load the file and the file gets loaded automatically, I'd say you've just answered your own question. But it's worth noting that auto-loading is intended to be used on functions and class definitions. You have an arbitrary code snippet that defines a variable and —as you've just learnt— since the variable becomes local to the auto-loader method it isn't of much use.
I'm trying to split up a long file into smaller chunks, so I created an src folder, and am trying to reference it from the main Extension.php file (which loads and works fine, by the way).
So, I add the src folder to the psr-4 autoloading array:
"psr-4": {
"Bolt\\Extension\\AndyJessop\\SurveyMonkey\\": [
"",
"src/"
]
}
I create the Test.php file inside src:
<?php
namespace Bolt\Extension\AndyJessop\SurveyMonkey;
class Test
{
public function test() {
return 'success';
}
}
In the Extension.php file (which is under the same namespace), I have this function that is called:
use Bolt\Extension\AndyJessop\SurveyMonkey\Test;
public function testing(){
return Test::test();
}
But I get the following error:
Error: Class 'Bolt\Extension\AndyJessop\SurveyMonkey\Test' not found
File: extensions/local/andyjessop/surveymonkey/Extension.php
First, either run composer update or composer dump-autoload to generate the autoload system.
Next, make sure that you include (require_once is preferable) the autoload at the top of your entrypoint(s):
require_once __DIR__ . '/path/to/vendor/autoload.php';
N.B.: if you have PHP 5.3 or lower, replace __DIR__ with dirname(__FILE__).
I am working with a folder structure as follows:
classes
-common.php
-core.php
modules
-index/index.php
I am trying to use the common.php in my index.php file and I am facing error:
Fatal error: Class 'classes\Common' not found in
D:\xampp\htdocs\devmyproject\modules\index\index.php on line 7
My Code:
commom.php class:
**Directory:/root/classes/common.php**
<?php
namespace classes;
class Common
{
function __construct()
{
}
}
My index.php file which try to use the classes/commom.php
**Directory:/root/modules/index/index.php**
<?php
namespace modules\beneficiary;
use \classes as hp;
include_once 'config.php';
$common = new \classes\Common();
//To Get Page Labels
$labels = $common->getPageLabels('1');
I am includeing common.php in config.php
$iterator = new DirectoryIterator($classes);
foreach ($iterator as $file) {
if ($file->isFile())
include_once 'classes/' . $file;
}
My Try:
It works fine when I use the folder structure as follows:
classes
-common.php
-core.php
modules
-index.php
If I use another folder inside modules it get error? I am not sure about he hierarchy of folders when using namespace can some one help me?
You have to either include common.php in index.php (better: use one of its relatives include_once or require_once) or set up an autoloader using spl_autoload_register() (or, not recommended, writing an __autoload() function).