How to access a package file form laravel controller - php

I am using gabrielbull/ups-api in my laravel project
composer.json as follows
"autoload": {
"psr-4": {
"App\\": "app/",
"Ups\\": "vendor/gabrielbull/ups-api/src"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
The controller code as follows :
use Ups\Rate;
$rate = new Ups\Rate($accessKey, $userId, $password);
but I am getting an error
Class 'App\Http\Controllers\Ups\Rate' not found

Your controller can't find the Ups\Rate.
You should be able to do:
$rate = new Rate($accessKey, $userId, $password);
If not: You should be able to debug this quick with the following code.
require __DIR__ . '/vendor/autoload.php'
use Ups\Rate;
new Rate()
echo Rate::class; // output

It's a PHP package, so once you install it via composer, it's already autoloaded. You don't have to mess with the composer.json file. After installing run:
composer dumpautoload

Related

Laravel 5.5 - ReflectionException (-1) Class does not exist

I am trying to use ReflectionClass to get file name(from app directory) from a controller. For testing whether I can use the ReflectionClass or not i was trying in following way:
In my MyController.php
public function readContent()
{
$files = app_path() . DIRECTORY_SEPARATOR. "Drama.php";
// It returns "F:\xampp\htdocs\projectDirectory\app\Drama.php"
$class = new ReflectionClass($files);
echo "file name:: ". $class->getFileName();
}
I have a Drama.php file in this path. But when I am running the route for this method, I get following error
ReflectionException (-1)
Class F:\xampp\htdocs\projectDirectory\app\Drama.php does not exist
I have updated my composer.json file like following:
"autoload": {
"classmap": [
"app",
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
So that, i can read my app directory files.
I also ran the following commands
composer dump-autoload
composer update
php artisan config:clear
php artisan cache:clear
But i am still getting this error. Can anyone tell me how can i resolve this ?
Your issue here is ReflectionClass takes the class path as the constructor argument, not file path. try new ReflectionClass('\App\Drama') instead.

Composer psr-4 autoload not working after deployment

I have my own little MVC framework and I use composer psr-4 autoloading.
On my own computer it works perfectly fine, but when I deployed it to my Ubuntu server it did not work anymore. (it doesn't find any classes anymore) I have tried a lot of things but it just won't work whatever I try...
What I have tried:
composer dump-autoload
composer update
removing everything and uploading again
searching on internet for a couple hours... :(
This is my composer.json:
{
"autoload": {
"psr-4": {
"App\\": "app",
"Core\\": "core",
"Magister\\": "vendor/Magister"
}
},
"require": {
"philo/laravel-blade": "^3.1"
}
}
I just don't get it why it's not working on my server....
I am using an other version of php on my server: 7.1, and I am using 5.6 on my computer, but this shouldn't make any difference right?
How do I fix this problem? I just don't get it why it happens.... :(
EDIT:
My code:
Index.php:
<?php
require "core/app.php";
$app = new \Core\App();
echo $app->start();
app.php:
<?php
namespace Core;
require "./vendor/autoload.php";
class App
{
function start()
{
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL ^ E_DEPRECATED);
$MC = new Routing();
// This is where it fails. Get the error: "class Core\Routing not found"
Routing.php:
<?php
namespace Core;
Use App\routes;
class Routing
{
private $parameters = [];
public function GetMC($Getroute){
}
}
File structure on server:
I have excluded the vendor map from the tree
okay... I have fixed it.
I have changed my composer.json to this:
{
"autoload": {
"psr-4": {
"App\\": "app/",
"Core\\": "core/",
"Magister\\": "vendor/Magister/"
},
"classmap": [
"app/",
"core/",
"vendor/Magister/"
]
},
"require": {
"philo/laravel-blade": "^3.1"
}
}
If you want to use psr-4 you need to capitalize your directories to
app
- Modules
- Controllers
- Views
-- Layouts
...
Please refer to this post as to why your autoloading isn't working.

Adding Third party library to Laravel

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

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

Categories