Adding Third party library to Laravel - php

I have an RSA algorithm Library giving to me by a payment gateway and When I do a
include (app_path().'/PaymentGateway/Crypt/RSA.php');
this and try to make an object as $rsa = new Crypt_RSA(); this it gives me and error saying
Class 'App\Http\Controllers\Crypt_RSA' not found
I tried including it in web.php and making an object it worked the problem occur when I try to include it in a Controller.

This is what I did. Oh and a little back ground I use to have this in Laravel 4, PHP 5, jpgraph 2.
I am using jpgraph 4.1 on Laravel 5.5 using PHP 7.
Created a folder under app called jpgraph
Placed the src folder that is in the tarball of jpgraph in that folder
Created file call Graph1.php, is my code using jpgraph, with the class Custom_GraphsJM in the jpgraph folder.
In composer.json added "app/jpgraph/Graph1.php" to the "classmap"
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/jpgraph/Graph1.php"
],
"psr-4": {
"App\\": "app/"
}
},
In the application folder:
composer dump-autoload
Checked the autoload_classmap.php and I have
'Custom_GraphsJM' => $baseDir . '/app/jpgraph/Graph1.php',
In my Model at the top I have
use Custom_GraphsJM;
To create a class
$Two_Graphs_Temp = new Custom_GraphsJM();

You can tell Composer to autoload any (non-PSR) class by adding the base folder to:
"autoload": {
"classmap": [
"app/commands",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
....
And you can also autoload autoloaders by adding them to the files section:
"autoload": {
"files": [
"temboo/src/Temboo_Loader.php"
],
...
After adding those entries, execute:
composer dumpautoload
And check the file vendor/composer/autoload_classmap.php, the available classes must be all listed in it, if one file is not there it will not be autoloaded.

On default, everything included in the app folder of your laravel project is autoloaded, that is described in the composer.json of your project:
...
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
...
The only thing you will need to do is simply use the namespace:
use App/Path/To/Third/Party/plugin/Class;
If, however, the plugin is placed outside of the scope of App, then simply add it to the psr-4 autoloader:
"psr-4": {
"ProjectRootNs\\": "projects/myproject/"
}

Related

Laravel modules in subfolder and composer

I'm using Laravel modules by nwidart and I want to create module in subfolder. According issue on its Github this is not possible but is possible to change paths in composer.json. So I've created new module Role2 and moved it to subfolder Administration. Then I've changed module's composer.json:
{
"name": "nwidart/role2",
"description": "",
"authors": [
{
"name": "Nicolas Widart",
"email": "n.widart#gmail.com"
}
],
"extra": {
"laravel": {
"providers": [
"Modules\\Administration\\Role2\\Providers\\Role2ServiceProvider"
],
"aliases": {
}
}
},
"autoload": {
"psr-4": {
"Modules\\Administration\\Role2\\": ""
}
}
}
But after command composer dumpautoload I see nothing in command php artisan module:list and also in vendor/composer/autoload_psr4.php there is no path to this module. What I'm doing wrong or what is missing to generate multiple composer (one in root and one in module)? Or is there some simple option to have module in subfolder?
There is what I have in root composer.json:
...
"autoload": {
"classmap": [
"database",
"vendor",
"app/Easyk"
],
"exclude-from-classmap": [
"vendor/swiftmailer"
],
"psr-4": {
"App\\": "app",
"Modules\\": "Modules",
"Easyk\\": "app/Easyk",
"Asipem\\OAuth2ClientManagement\\": "packages/asipem/oauth2-client-management/src",
"Asipem\\CAPRequestDispatcher\\": "packages/asipem/cap-request-dispatcher/src"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
],
"psr-4": {
"Tests\\Unit\\": "tests/unit"
}
},
...
autoload -> psr-4 key from composer.json is used to define psr-4 root namespaces for the application.
What happens is you define a namespace and the directory location for Autoloader to look for the files under that namespace.
As in your root composer.json
"psr-4": {
"App\\": "app",
"Modules\\": "Modules",
"Easyk\\": "app/Easyk",
"Asipem\\OAuth2ClientManagement\\": "packages/asipem/oauth2-client-management/src",
"Asipem\\CAPRequestDispatcher\\": "packages/asipem/cap-request-dispatcher/src"
}
App is the base namespace and app is its base directory. By defining it, you tell autoloader that all files with namespace App will be residing inside the directory 'app'. After that, Autoloader will traverse the app directory in the PSR-4 way for file path following the namespace trail after base namespace App.
For example: Controller App\Controller\ExampleController will tell Autoloader that the class defination will be found at app/Controller/ExampleController.
So you need to specify the base directory location of your module against the namespace Modules\\Administration\\Role2\\ which is the base source directory of your module folder.
Hope it helped.

Create function for use in views and controllers

Im relative new in laravel, before use laravel I used a file functions.php with all functions that I use in my projects (create slugs, format dates, etc).
where I can create my own functions in laravel to use in controllers and views like myfunction($data) ?
Use the Laravel composer example, where it creates some helper files:
"autoload": {
"files": [
"src/Illuminate/Foundation/helpers.php",
"src/Illuminate/Support/helpers.php"
],
"psr-4": {
"Illuminate\\": "src/Illuminate/"
}
},
Edit your composer.json file, where you could create a helper in the App folder
"autoload": {
"files": [
"app/helpers.php",
],
...
},
Then you just have to execute
composer dumpautoload
To tell composer to load it automatically.
In your app/Http directory, create a helpers.php file to add your functions
Within composer.json, in the autoload block, add:
"files": ["app/Http/helpers.php"].
On command line run "composer dump-autoload"

Add an external library to symfony

I'm trying to add an external library to symfony.
I've tried this on the app/autoload.php:
$loader->add('LibCokeId',__DIR__ . '/../vendor/libcokeid/libcokeid/lib');
However when I try to use it in a controller:
use libCokeId\LibCokeId
Libcokeid::init()
I get the miss use statement error.
Any help?
In the situation where you have a library that doesn't use composer and you can't retrieve it from packagist, you can manipulate the Composer autoload.
Simply add the class in the composer.json files, as example:
"autoload": {
"psr-0": { "": "src/" },
"files": [
"vendor/folder/my_custom_lib/myFiles.php",
"vendor/libcokeid/libcokeid/lib/libCokeId/LibCokeId.php"
]
},
OR you can Autoload the whole folder in composer.json:
"autoload": {
"psr-0": { "": "src/" },
"classmap": [
"vendor/libcokeid/libcokeid/lib"
],
},
Remember to make a composer install after setting this.
Hope this help.

Composer/Laravel Fatal Error

I added this to my composer.json file
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Claremontdesign\\Cdbase\\": "packages/Claremontdesign/cdbase/src",
"Claremontdesign\\Narbase\\": "packages/Claremontdesign/narbase/src",
"Claremontdesign\\Nhr\\": "packages/Claremontdesign/nhr/src"
},
"files": [
"packages/Claremontdesign/cdbase/src/Helpers/helpers.php",
"packages/Claremontdesign/narbase/src/Helpers/helpers.php",
"packages/Claremontdesign/nhr/src/Helpers/helpers.php"
]
},
then, I ran composer update from the command line, and it gave me this error:
symfony component debug exception fatalerrorexception class "Claremontdesign\Cdbase\ServiceProvider" not found
Has anyone else encountered this?
Also, I added a service provider in add.php
Claremontdesign\Cdbase\ServiceProvider::class
Did you try running just composer dump? composer update runs some scripts before it actually runs - for example php artisan clear-compiled. When artisan runs, it'll probably fail because it tries to register the serviceprovider which isn't autoloaded yet. composer dump only generates the autoload files, which is what you need in this case.
i think you are missing one more slash after every src folder
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Claremontdesign\\Cdbase\\": "packages/Claremontdesign/cdbase/src/",
"Claremontdesign\\Narbase\\": "packages/Claremontdesign/narbase/src/",
"Claremontdesign\\Nhr\\": "packages/Claremontdesign/nhr/src/"
},
"files": [
"packages/Claremontdesign/cdbase/src/Helpers/helpers.php",
"packages/Claremontdesign/narbase/src/Helpers/helpers.php",
"packages/Claremontdesign/nhr/src/Helpers/helpers.php"
]
},

Composer dump-autoload, issue

While Working on a project using Laravel 4 to be precise, I decided that i wanted to make my own helper file to house my custom functions.. one of which is this below...
function pr($ar=array(), $bool=false){
echo '<pre>';
print_r($ar);
echo '</pre>';
if($bool){
exit;
}
}
in my composer.json file, just after the autoload: classmap , i added myne, autoload:files -arrar and included my custom file, app/helpers as illustrated below..
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"others":[
"app/helpers.php"
]
and i switched to my terminal window and ran the following commands
composer dump-autoload -o
but i still got errors that my pr() function was undefined... then i tried the artisan alternative... [-o ] to optimize the files
php artisan dump-autoload
but still it refused to work... and then i changed the array name from
"others":[
"app/helpers.php"
]
to
"files":[
"app/helpers.php"
]
then i got the desired response, my code could now see the custom function i wrote, please i'd like to know if there is a pattern i was supposed to follow or otherwise, in my case, i mistook " files ", for " others " and i got errors, but incase, what did i miss here, all i see is just a name-string value for the array representation....
This is how composer works. In autoload section you need to use files when you want to load some files. In my Laravel 5 project I have for example:
"autoload": {
"classmap": [
"database",
"tests/TestCase.php"
],
"psr-4": {
"App\\": "app/",
"verify\\": "verify/"
},
"files": [
"app/Helpers/functions.php"
]
},
If you look at documentation you will see that you need to use files to load any extra files by autoloader.
According to the official documentation
Currently PSR-0 autoloading, PSR-4 autoloading, classmap generation
and files includes are supported. PSR-4 is the recommended way though
since it offers greater ease of use (no need to regenerate the
autoloader when you add classes).
So the reason that "others" did not work was because it is not supported by composer. "others" is simply meaningless, while "files" actually have a specific autoloading mechanism.

Categories