Laravel 4: loading an old library: how? - laravel

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

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.

How to create custom helper file in laravel 5.6?

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.

Laravel 4.2 , Model (eloquent) my own class not found

I have this weird error ... I apparently cannot use any of my model class in my project..
Ad_category model
class Ad_category extends Eloquent {
protected $table = 'ad_category';
protected $fillable = array('*');
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
}
calling this
$ad_cat=Ad_category::find(1);
error
`Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_ERROR)
Class 'Ad_category' not found `
PHP frameworks use a system called "autoloading" to automatically include or require in the correct class definition file when you want to use a class. Autoloading in Laravel 4.2 is in a bit of a transitional spot, which means there's multiple answers to your question.
By default, Laravel 4.2 will look for a class named Ad_category in one of the following four locations.
app/commands/Ad/category.php
app/controllers/Ad/category.php
app/models/Ad/category.php
app/database/seeds/Ad/category.php
That is, Laravel's autoloader will automatically convert Ad_category into the file path Ad/category.php, and then check each configured autoload path for that file. You can configure the base autoloader paths in
#File: app/start/global.php
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
));
Laravel 4.2 also uses composer based autoloading. Specifically, is uses a very aggressive form of composer autoloading called classmap autoloading. If you look in your composer.json file, you'll see a section like this
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
When you manually run the command
$ composer dumpautoload
Composer will go through every folder in the above section and look for PHP class files. If it finds one, it adds it to the classmap in vendor/composer/autoload_classes.php. Composer also runs this command automatically during updates.
So, what this means is, if you've defined Ad_category in a different location than Laravel expects to find it, you may be able to get away with running
$ composer dumpautoload
and Laravel will use Composer's autoloader to find your class.

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?

Created package in Laravel workbench, but how to transfer to vendor folder?

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

Resources