3rd Party class in laravel - php

Here is what I've done so far.. based on this link Laravel cannot load 3rd party library
I followed all of it but still Im having this error
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Class 'FileProcess' not found","file":"C:\\xampp\\htdocs\\fileshare\\trunk\\app\\controllers\\UserFilesController.php","line":437}}
my composer.json
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/library",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
then my controller
<?php
use \FileProcess;
class UserFilesController extends \BaseController {
public function someMethod(){
$fp = new FileProcess;
}
}
then my 3rd party class which is located in app/library/FileProcess.php folder
<?php namespace FileProcess;
class FileProcess
{
// some methods
}
i do not know what is wrong or if there is lacking

The reason Laravel can't find the class is because you have namespaced it and when you call it using use you are calling it from the Global namespace. Either of the following will fix it for you.
1) Remove namespace FileProcess; from the class file
2) In your controller call it using it's namespace use FileProcess\FileProcess;

Related

Laravel not finding the controller located in other folder

I'm trying to implement a clean architecture in laravel, thus I'm moving my own code to a src folder.
My controller is located in src\notebook\infrastructure but when i call it from routes\web.php this way:
Route::get('/notebook', 'src\notebook\infrastructure\NotebooksController#show');
i got this error:
Illuminate\Contracts\Container\BindingResolutionException
Target class [src\notebook\infrastructure\NotebooksController] does not exist.
http://127.0.0.1:8000/notebook
i also changed the namespace value in the class RouteServiceProvider from:
protected $namespace = 'App\Http\Controllers';
to
protected $namespace = '';
This is my notebook controller class:
namespace src\notebook\infrastructure;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class NotebooksController extends Controller
{
public function show($id)
{
echo 'controller from infrastructure folder';
}
}
My laravel and php version in composer.json are:
"php": "^7.2.5",
"laravel/framework": "^7.24",
I feel like i'm missing something stupid but can't figure it out what.
Did you add src folder into the autoload?
In composer.json file, you must have something like this:
"autoload": {
"psr-4": {
"App\\": "app/",
"Src\\": "src/" // add this
},
"classmap": [
"database/seeds",
"database/factories"
]
},
After changing it run composer dump-autoload.
And also don't forget to follow psr-4 rules and use Studly case namespace and class names.
namespace Src\Notebook\Infrastructure;

psr-4 autoloading not working in Laravel 4

I'm trying to create a directory to store custom classes, so I create the directory app/ArgumentClub/Transformers, and the class UserTransformer.php in that folder.
I then autoload with:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-4": {
"ArgumentClub\\": "app/ArgumentClub"
}
},
And run composer dump-autoload. And namespace like this:
<?php namespace ArgumentClub\Transformers;
class UserTransformer {
I'm calling this class within another class like this:
<?php
use Sorskod\Larasponse\Larasponse;
use ArgumentClub\Transformers;
class UsersController extends \BaseController {
...
$transformed = $this->fractal->collection($users, new UserTransformer());
But I get the error:
Class 'UserTransformer' not found
What am I doing wrong here?
You're not using the use correctly.
use ArgumentClub\Transformers; imports that Namespace, but doesn't import the class you want to use.
To fix it you can either extend the use statement (which you should) to be like so:
use ArgumentClub\Transformers\UserTransformer
Or you can add the Transformers namespace to where you instantiate your UserTransformer class
$transformed = $this->fractal->collection($users, new Transformers\UserTransformer());
When you want to instantiate a namespaced class without putting the full namespace, you need to put the full class path in the use statement.

having trouble psr-4 autoloading in Laravel

Okay so I have an project folder inside my Laravel app. named TLGD (the name if my site). Inside i created a form validation helper which is being used to take away unnecessary code form the controller.
here is the folder structure:
TGLD\Validation\Forms
and inside here I have my form helper classes
now in the controller just to test it out I was calling the classes by the use methd from php like so:
use TGLD\Validation\Forms\Login;
login being the class for the login validation
now thus works great so I tried to autoload the TGLD folder so I don't need to add the use line in every controller. Here is my composer.json file
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-4": {
"TGLD\\": "app/TGLD"
}
},
but when I autoload it it gives me the error that my login class doesn't exist which means the autoloader is not working. Is there a syntax error or am I missing something? I ran
composer dump-autoload -o
well any advice is helpful thank you in advance
What you want to do cannot be done.
You have to use the complete namespace somewhere to identify the class you want to use. You have several options to do this
Using the fully qualified name of the class with namespace like this: new \TGLD\Validation\Forms\Login().
Using a use clause that imports the named class into the current namespace for this file with use TGLD\Validation\Forms\Login; ... new Login();
Using a part of the namespace in the use, and use the rest in the class name: use TGLD\Validation\Forms; ... new Forms\Login();
After you have chosen the class name in one way or the other, PHP knows which class it needs, and then triggers the autoloading if the class is still unknown to PHP.
So you cannot affect the naming of classes with autoloading.

Laravel - Loading classes

There are questions on here and around Google asking about the same thing, but being a noob, I'm still not getting this. I'm using Laravel 4.
Trying to have a file for random classes. Doesn't load.
The class is in:
app/classes/Helpers.php
Helpers.php:
class Helpers {
public function randomLowerCase($amount)
{
strtolower($str_random($amount))
}
};
I've placed my classes in composer.json.
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/classes",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
autoload_classmap.php:
'Helpers' => $baseDir . '/app/classes/Helpers.php',
And also ran
composer dump-autoload
I'm running the function in a UserController.php file in controllers, but I keep getting Call to undefined function randomLowerCase()
The problem is that you're not instantiating an instance of the Helpers class before you call one of its methods. You'll want to do one of the following:
First, keeping your class as it is, you could create an instance in the controller and call your method on it:
// Controller
$helpers = new Helpers;
$helpers->randomLowerCase($str);
Or, you could make the method static and call it as a static method:
// Helpers.php
class Helpers
{
public static function randomLowerCase($amount)
{
strtolower($str_random($amount))
}
};
// Controller
Helpers::randomLowerCase($str);
The error you're getting is because you're running the randomLowercase method as if it were just a function; methods are functions attached to a class/object.

Custom validation class not loading in Laravel 4

I'm trying to write my own validation class in Laravel 4. To do this, I created a new directory called app/validators. I then added this directory to the composer.json classmap and ran composer update, like so:
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/validators", <- added here
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
My validation class looks like this:
class LinkValidation extends Illuminate\Validation\Validator {
{
public function validateHost($field, $value, $params)
{
return $value == 'test';
}
}
and in my controller, I'm trying to extend the validator like so:
Validator::extend('awesome', 'LinkValidation#supportedHost');
However, I'm getting this error when loading a page:
{
"error":{
"type":"ReflectionException",
"message":"Class LinkValidation does not exist",
"file":"C:\\xampp\\htdocs\\laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php",
"line":301
}
}
Any ideas why Laravel won't load the class? I thought it'd do it automatically if added to composer's classmap.
Not sure why it isn't working, but I think you should try adding it in app/start/global.php.
/*
|--------------------------------------------------------------------------
| Register The Laravel Class Loader
|--------------------------------------------------------------------------
|
| In addition to using Composer, you may use the Laravel class loader to
| load your controllers and models. This is useful for keeping all of
| your classes in the "global" namespace without Composer updating.
|
*/
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/validators',
));
Always do composer dumpautoload after create new class object.

Categories