Cannot find class in vendor folder codeigniter - php

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"
}
}

Related

Load class from vendor folder inside autoloaded class from a folder outside vendor

I have a php application with a folder structure like this
-/app
-/vendor
-index.php
My "vendor/autoload.php" file is included in "index.php" file. When I call a class from vendor for example upload() in "index.php" file it loads without a problem. However if I call the same class in a method from a class inside "app" folder it shows class not found error.
Classes inside "app" folder are autoloaded like this
"autoload": {
"psr-4":{
"App\\": "app/"
}
},
"require": {
"verot/class.upload.php": "dev-master"
}
How can I call classes from vendor folder inside autoloaded classes in app folder?
Edit:
Classes from app folder are called in "index.php" like this
include("vendor/autoload.php");
$get_class = 'User';
require_once('app/'.$get_class.'.php');
$get_class = 'App\\'.str_replace('/', '\\', $class_name);
if(method_exists($get_class , 'uploadImage')) {
$class = new $get_class();
$class->{ 'uploadImage' }();
}
Here is the upload() class being called in User class
namespace App\User;
class User{
public function uploadImage()
{
$file = 'user.jpg';
$handle = new upload($file);
}
}
Here is the error message
Class 'App\upload' not found in app/User.php:20
This is a namespace issue. You need to either add:
use upload;
under the namespace declaration in you App\User-file to import the upload-class or you need to use the full namespace to the upload class when using it:
$handle = new \upload($file);
You can read more about it in the manual
Note: In the posted code, $file is undefined when you're trying to use it in uploadImage()-function.
Note 2: If you've included vendor/autoload.php in the top of your index.php (which you should), there's no need to include the classes manually in PHP. Composers autoloader will handle that automatically. Just to: $user = new App\User.

Mapping home controller as the default controller in AltoRouter

This is the index.php
<?php
include 'library/altorouter.php';
$router = new AltoRouter();
$router->setBasePath('/AltoRouter');
$router->map('GET','/', 'home_controller#index', 'home');
$router->map('GET','/content/[:parent]/?[:child]?', 'content_controller#display_item', 'content');
$match = $router->match();
// not sure if code after this comment is the best way to handle matched routes
list( $controller, $action ) = explode( '#', $match['target'] );
if ( is_callable(array($controller, $action)) ) {
$obj = new $controller();
var_dump($obj);
call_user_func_array(array($obj,$action), array($match['params']));
} else if ($match['target']==''){
echo 'Error: no route was matched';
} else {
echo 'Error: can not call '.$controller.'#'.$action;
}
// content_controller class file is autoloaded
class home_controller {
public function index() {
echo 'hi from home';
}
}
and it works good. The home_controller class is supposed to be the default controller.
Problem is, when I remove the class home_controller
class home_controller {
public function index() {
echo 'hi from home';
}
}
and save it as a seprate file home_controller.php in app/controller directroy it does not work.
I understand that the router is unable to locate the home_controller class hence will not show it's content (if i directly include the file home_controller.php it again works as normal).
My question is, how do you map the home_controller as default, which is in a different directory?
It looks like you're not using composer for installing the package. It's standard way in PHP.
1. Install Composer
Here is manual, depending on your OS
2. Call Composer from Command Line
Go to your root directory of your project, open Command Line and type:
composer require altorouter/altorouter
You'll find the package name altorouter/altorouter in composer.json on Github page of package - here.
3. Add loaded files to your index.php
Now you have installed router package. Next step is adding all composer loaded files to your application. Just replace include 'library/altorouter.php'; with following:
<?php
# index.php
require_once __DIR__ . '/vendor/autoload.php';
4. Load Your Controllers by Composer as well
Last step is tell composer where to find your classes.
Open composer.json and add following section:
{
"autolaod": {
"classmap": ["app"]
}
}
Read more about classmap option in documentation.
To update /vendor/autoload.php with this option, just call from command line:
composer dump-autoload
That should be it. If you come at any troubles, let me know which point.

Undefined method with Composer library and CodeIgniter

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');

Composer autoloaded files debug [duplicate]

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

How can i use Symfony or Zend Components inside Code Igniter?

I would like if there is a simple and clean way to use ZF´s (or Symfony´s) component library in a CodeIgniter application, because there is a lot of good and useful stuff there that CI don´t have.
It´s possible. For example for ZF, you would need to do something like:
1) Setup CodeIgniter.
2) Download the Zend Framework. Extract the archive.
3) From the ZF files, copy the “Zend” directory from inside the “library” directory.
4) Paste the directory into the “system/application/libraries” directory.
So ultimately, the new location of the copied “Zend” directory would be “system/application/libraries/Zend”.
If you’re on Linux/Unix, we probalby will need to deal with file permissions.
I guess you will need to make the Zend directory accessible by all (use chmod).
5) In the same “system/application/libraries/” directory, create a new file named “Zend.php” and put the following contents:
<?php if (!defined('BASEPATH')) {exit('No direct script access allowed');}
class Zend
{
function __construct()
{
ini_set('include_path',
ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'libraries');
}
function load($class)
{
require_once (string) $class . EXT;
}
}
?>
Now test the setup using the CI´s default Welcome controller
<?php
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
$this->load->library('zend');
$this->zend->load('Zend/Service/Flickr');
$flickr = new Zend_Service_Flickr('12e99caebb8f305fff5a943606ecde18');
$results = $flickr->tagSearch('worldcup');
foreach ($results as $result)
{
$photo = $result->Small;
echo "<img src=\"{$photo->uri}\" /><br /><br />";
}
}
}
?>
You can use components symfony by installing them via composer.
For example, you can install the varDumper component which provides a better dump() function that you can use instead of var_dump.
composer require symfony/var-dumper
the composer will install the dumper component with all his dependecy
then include the autoload and use the dump function
require __DIR__.'/vendor/autoload.php';
// create a variable, which could be anything!
$someVar = ...;
dump($someVar);

Categories