I am using the FPDI library from JanSlabon for securing PDF file uploads from my laravel app. But I can't execute the code require_once even though I navigated to the file itself. I am getting the error:
Failed opening required '../../vendor/setasign/fpdf/fpdf.php' (include_path='.:/usr/local/Cellar/php/7.3.4/share/php/pear')
My require code is:
require_once('../../vendor/setasign/fpdf/fpdf.php');
require_once('../../vendor/setasign/fpdi/src/autoload.php');
When the libraries are already located in your vendor folder, you should simply make use of the autoload.php file of composer (doesn't laravel uses this by default?).
So just add the dependencies to your composer.json (if not already done):
"require": {
"setasign/fpdf": "^1.8",
"setasign/fpdi": "^2.2",
"setasign/fpdi-protection": "^2.0"
}
Update via composer update and:
<?php
use setasign\FpdiProtection\FpdiProtection;
require_once('vendor/autoload.php');
$pdf = new FpdiProtection();
...
Your relative path ../../ to vendor is probably wrong. To avoid this issue, use the Laravel base_path() helper which will provide an absolute path.
require_once(base_path('vendor/setasign/fpdf/fpdf.php'));
require_once(base_path('vendor/setasign/fpdi/src/autoload.php'));
You can autoload using composer.json. First of all, create a directory called Custom in app directory and copy fpdi directory to app/Custom.
Now in autoload section of your composer.json file, require the file. After requiring the file, your composer.json file's autoload block should look like this if it is a fresh Laravel app:
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
],
"files": [
"app/Custom/fpdi/FPDI_Protection.php"
]
},
After updating your composer.json file, run composer dumpautoload. Now you can utilize the classes in your Laravel controllers or models without requiring the files manually.
While doing tests, I see that this library uses some deprecated methods and so on. You will have to deal with it, i.e. update the code to suite your needs. But I hope that this answer will help you in a way that you will be able to use any other library as well. Do a Google search and find a more modern library if this one's fixes are too broad.
Related
When I create an autoload helper file with composer following error ocures:
Fatal error: composerRequireebefe31fc60fbe2897fba8c156ec310c(): Failed
opening required
'D:\xampp\htdocs\common_module\vendor\composer/../../app/Http/helpers.php'
(include_path='D:\xampp\php\PEAR') in
D:\xampp\htdocs\common_module\vendor\composer\autoload_real.php on
line 66
Following are steps to create a Custom Helper In Laravel 5.5
Step : 1 Create app/helpers.php file
First, create one helper class file in app/helpers.php path and in this file we are write our any custom helper logic into the function.
Step : 2 Add app/helpers.php file in composer.json file
Now, we are add our app/helpers.php file in composer.json file for a autoload section.
"autoload": {
"classmap": [
...
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/helpers.php" //Add This Line
]
},
After done this then once we are run following command.
composer dump-autoload
for more visit following link
Visit How To Create Custom Helper In Laravel 5.5
I hope this will help you...
In older version of Laravel I do the following:
app folder create a Helpers folder
Create a class YourHelper class file
in that class file set namespace to App\Helpers
Name the class file
in config/app.php in aliases create an alias (so you can call the helper in your view also) 'YourHelper'=>'App\Helpers\YourHelper::class'
If you are new to Laravel or PHP, let’s walk through how you might go about creating your own helper functions that automatically get loaded by Laravel.
Creating a Helpers file in a Laravel App
you can organize the location of your helper file(s) however you want, however, here are a few suggested locations:
app/helpers.php
app/Http/helpers.php
I prefer to keep mine in app/helpers.php in the root of the application namespace.
Autoloading
PHP functions cannot be autoloaded. However, we have a much better solution through Composer than using require or require_once.
So composer has a files key (which is an array of file paths) that you can define inside of autoload:
"autoload": {
"files": [
"app/helpers.php"
],
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
Once you add a new path to the files array, you need to dump the autoloader:
composer dump-autoload
(Use this command after changing composer.json.)
Now on every request the helpers.php file will be loaded automatically because Laravel requires Composer’s autoloader in public/index.php:
require __DIR__.'/../vendor/autoload.php';
Defining Functions
Defining functions in your helpers class is the easy part, although, there are a few caveats. All of the Laravel helper files are wrapped in a check to avoid function definition collisions:
if (! function_exists('env')) {
function env($key, $default = null) {
// ...
}
}
I prefer to use function_exists checks in my application helpers, but if you are defining helpers within the context of your application, you could forgo the function_exists check.
I am new developer on Laravel, now I'm using Laravel version 5.5
I got the problem after used php artisan app:name on my project, I got the problem:
In ProviderRepository.php line 208: Class
'App\Providers\AppServiceProvider' not found
as the captured image below:
As this error, I can not use php artisan commands or composer commands anymore can you guys please help me to solve this problem I am really appreciated for time. Thanks you
Best Regards
Siripong Gaewmaneechot
I would suggest looking in your config files, and the main classes which were generated when you started your laravel project (User class, etc) because they are all set to App\User App..... etc.
So for example, in the image you have in the question, it says it can not find App\AppProviders... - This indicates that somewhere you still have a use statement pointed to App\AppProviders.. but you changed the app name, so it's no longer App. something I do if I made that mistake, is I do a global search in my project files for App\ (you may need to put App\\ in the search because \ is an escape character
So if you did not change the app name immediately after starting the project, some of the paths will not be pointing to the correct directories. Let me know if that makes sense.
The command changes the PSR-4 configuration in composer.json.
Assume it was App before, your composer.json looks like this:
"autoload": {
"psr-4": {
"App\\": "app/"
}
},
After running the command with php artisan app:name Foo, it will look like:
"autoload": {
"psr-4": {
"Foo\\": "app/"
}
},
Therefore the whole namespace has changed and your classes can't be found by the Autoloader. To fix this, you have to either go back to the old name or do a global search and replace to change the namespace from App to Foo.
I have create a new directory Library in path App\Http
When I try to use this path to load class pagination:
use App\Http\Library\Pagination;
Then IDE says that does not see path Library
You need to autoload this into class map. In composer.json put the class into classmap.
"autoload": {
"classmap": [
"app/Http/Library/Pagination"
]
},
Then you update your autoload file composer-dumpautoload or php artisan optimize
Let's say my package in Laravel is test/test.
I created the package in the workbench and it's been working great following Jason Lewis' tutorial. Now I want to move the package out of
the workbench to the vendor directory. This is where all tutorials fall short, even the laravel docs. I didn't want to use git to move the files, so I simply copied the test/test package from the workbench to the vendor directory (and then deleted it from the workbench). I didn't copy the test/test/vendor folder from the workbench (or any other files I noticed in the .gitignore file). I then ran composer install from my new vendor/test/test directory. I then did a composer dump-autoload from the laravel root directory.
Now when I run my application I get an error that I did not get when the package was in the workbench:
Class 'Test\Test\TestServiceProvider' not found
(this is coming from \bootstrap\compiled.php on line 4121)
I also did a php artisan dump-autoload from the laravel root and I get this same error.
Any ideas? Or can someone lead me to a tutorial that takes the package development all the way to it's final resting point in the vendor directory?
Got it working.
I added:
"psr-0": {
"Test\\Test": "vendor/test/test/src/"
}
to the autoload section in composer.json in the laravel root directory so it looks like this:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-0": {
"Test\\Test": "vendor/test/test/src/"
}
},
If I decide to put the package on Packagist later then I might remove this from the autoload and just keep the package referenced in the "require" part of my composer.json. We'll see what happens when I get that far!
I think you can install your packages from your hard drive as from local repository like this:
"repositories": [
{
"type":"vcs",
"url":"/path/to/repo/here"
}
],
"require":{
"me/myrepo":"dev-master"
}
I have an old library (phpquery) that I'd like to include in my project. I've put it inside vendor, but it doesn't work, as it's not PSR-0 compliant. I don't want it to load for every request, so I didn't put the require inside bootstrap autoload.php.
I don't even know, how I can get the root of the app. Running path() gives me a URL, not what I'm after.
So how can I do that?
You can create a libraries directory just like in laravel 3 and include it in your class loader. You can do this via composer or laravel.
//composer.json
"autoload": {
"classmap": [
"app/commands",
"app/libraries",
"app/database/migrations",
"app/tests/TestCase.php",
]
},
//app/starts/global.php
ClassLoader::register(new ClassLoader(array(
app_path().'/libraries',
)));
Autoloading via Laravel does not require you to run "composer dumpautoload" every time a class is created or removed.
UPDATE - L4 Beta 4
ClassLoader::addDirectories(array(
app_path().'/libraries',
));