How to create custom helper file in laravel 5.6? - laravel

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.

Related

Can't require_once fdpf/fpdf.php

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.

Can’t seed data from my Laravel plugin

I developed a Laravel Plugin, which contains seed data : https://github.com/xoco70/laravel-tournaments
When I try to seed it inside a fresh laravel 5.6 project, with:
php artisan db:seed --class=LaravelTournamentSeeder
I get:
ErrorException : include(/Users/julien/Documents/Proyectos/test/vendor/composer/../../database/seeds/LaravelTournamentSeeder.php): failed to open stream: No such file or directory
When I try to namespace all my seeds file with :
namespace Xoco70\LaravelTournaments\Database\Seeds;
and then ruin it with
php artisan db:seed --class=Xoco70\\LaravelTournaments\\Database\\Seeds\\LaravelTournamentSeeder
I also get an error:
ReflectionException : Class Xoco70\LaravelTournaments\Database\Seeds\LaravelTournamentSeeder does not exist
Inside my plugin, my composer.json is:
…
"autoload": {
"psr-4": {
"Xoco70\\LaravelTournaments\\": "src"
},
"classmap": [
"src/"
]
},
…
What am I doing wrong ?
I can see in the GitHub code that the seeder class is not namespaced neither the other seeding files. The missing bit is to move the database related code under "src" following the psr-4 entry in the composer:
"psr-4": {
"Xoco70\\LaravelTournaments\\": "src"
},
Another alternative is to add another entry in the psr-4 but it might be not clear.

How to get access to directory in Laravel?

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

How to use external php classes in laravel framework?

I'm switching from Codeigniter to Laravel. In CI its easy to user external libraries or classes, just put it in application/libraries folder then load it from your controller by:
$this->load->library('libraryName')
Now, I wanna do the same thing in Laravel. I used this scenario :
Create a folder inside my app folder called libraries.
Change the composer.json (which located in the root) as the following:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/libraries" // this was added
]
},
And from CMD run the command composer dump-autoload.
My class look like this:
class Messages{
function errorMessage(){//....}
function successMessage(){//....}
}
Class stored in file called Messages.php which located in app\libraries folder.
I still have the same error:
Class 'Messages' not found
What is the problem and what is the solution?
Create a new folder: app/your-folder-name and put your class in that folder
Open the globals.php file in app/start
You should see this:
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
));
Add your folder with your class to this array
app_path().'/your-folder-name'
More info can be found here: Where do I put Laravel 4 helper functions that can display flash messages?
check your file with autoloading classes /vendor/composer/autoload_classmap.php
your class should in there.
if it is - check do you actually using composer autoloading on your request?

Laravel 4: loading an old library: how?

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

Resources