I have currently added a new folder to my app directory for all my 'libraries'. I keep recieving an error that the class is not found, this is what I do:
I have added it to the composer.json file in the autoload value:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/services",
"app/facades",
"app/libraries" --> here
]
},
Added it to my global.php file in the classloader:
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/libraries', --> here
));
Created an alias for it in app.php:
'aliases' => array(
'App' => 'Illuminate\Support\Facades\App',
'Session' => 'Illuminate\Support\Facades\Session',
...
'SimpleImage' => 'App\Libraries\abeautifulsite\SimpleImage', --> here
),
And called the class in my controller:
$img = new SimpleImage($file);
The error I keep receiving is that the class is not found. What am I missing?
ErrorException (E_UNKNOWN)
Class 'App\Libraries\abeautifulsite\SimpleImage' not found
(PS. I did composer dump-autoload in terminal)
The best way to add a custom folder in your Laravel project is using PSR-4.
First of all create your custom directory in /app, for example:
/app/YourName/libraries
now open composer.json and just after the autoload -> classmap add the psr-4 structure:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/services",
"app/facades"
],
"psr-4": {
"YourName\\": "app/YourName"
}
},
Note that I removed your /app/libraries row, then run:
composer dump-autoload
In this way all the folders you will add in YourName folder will be loaded.
For example:
app/ChristianGiupponi/libraries
and
"psr-4": {
"ChristianGiupponi\\": "app/ChristianGiupponi"
}
Related
Composer.json
"autoload": {
"exclude-from-classmap": [
"vendor\\laravel\\passport\\src\\Http\\Controllers\\AccessTokenController.php"
],
"psr-4": {
"App\\": "app/",
"Laravel\\Passport\\": "app/Override/" //tried "Passport//" only also
}
},
I have file AccessTokenController.php which lie in vendor\laravel\passport\src\Http\Controllers\ and i create Override folder inside App directory on where i copied that file and make changes in code
And finally when i do :
composer dump-autoload
i get error as :Class Laravel\Passport\Http\Controllers\AccessTokenController located in ./app/Override/AccessTokenController.php does not comply with psr-4 autoloading standard. Skipping.
seems problem with namespace which i couldn't figure out though trying some of the ways ..
Anyone can help me ?
I would simply override the route from within the app as mentioned here. No need for the approach you mentioned above.
However, if you want to keep going this route you can try the following.
"psr-4": {
"App\\": "app/",
}
"exclude-from-classmap": [
"Laravel\\Passport\\Http\\Controllers\\AccessTokenController"
],
"files": [
"app/Override/AccessTokenController.php"
],
I solved it by including complete namespace of AccessTokenController.php.so final code would look like this:
"autoload": {
"exclude-from-classmap": [
"vendor\\laravel\\passport\\src\\Http\\Controllers\\AccessTokenController.php"
],
"psr-4": {
"App\\": "app/",
"Laravel\\Passport\\Http\\Controllers\\":"app/Override/"
}
}
In my following composer.json I am requiring extensions, which are in the same Git repository as the whole project. So I add in the repositories section and later I do composer req vendor/site_package:#dev in order to require my local extension.
Now I realized, that some classes of the extension are not autoloaded.
Do I need to additional add the autoload part as shown below in the composer.json of the project?
{
"name": "site-package",
"description": "Base composer.json",
"repositories": [
{
"type": "path",
"url": "./packages/*"
}
],
"require": {
"typo3/cms-backend": "^10.4",
"typo3/cms-belog": "^10.4",
"typo3/cms-beuser": "^10.4",
"typo3/cms-core": "^10.4",
...
"vendor/site_package": "#dev",
"georgringer/news": "^8",
...
},
"autoload": {
"classmap": [
"public/typo3conf/ext/site_package/Classes"
],
"psr-4": {
"Vendor\\SitePackage\\": "public/typo3conf/ext/site_package/Classes"
}
},
"extra": {
"typo3/cms": {
"root-dir": "public",
"web-dir": "public"
}
},
"config": {
"vendor-dir": "vendor",
"bin-dir": "bin"
},
"scripts": {
"typo3-cms-scripts": [
"typo3cms install:generatepackagestates",
"typo3cms install:fixfolderstructure"
],
"post-autoload-dump": [
"#typo3-cms-scripts"
]
}
}
In ext:site_package I have the following autoload section as well:
"autoload": {
"psr-4": {
"Vendor\\SitePackage\\": "Classes",
}
},
Do I need both? Why?
You should never mix those 2 composer.json.
As you already follow the approach of having a directory packages (which is a good thing) each extension inside there needs an own composer.json file which of course also needs a section
"psr-4": {
"Vendor\\MyExt\\": "Classes"
}
By requiring this package, this autoload information will be used.
If you would still have the custom extension inside typo3conf/ext/my_ext, that composer.jsonfile would not be taken into account and you need something like
"psr-4": {
"Vendor\\MyExt\\": "typo3conf/ext/myext/Classes"
}
The autoload part is only needed in your site package composer.json. It's not necessary to put it also in the composer.json in your root folder.
Please refer to the documentation how the composer.json of your site package should look like.
If you still have problems with autoloading, try composer dump-autoload or remove the extension and require it again. And make sure to check the upper-/lowercase of your namespace. It's case sensitive. If you change that after you required the site package, you need to remove and require the package again.
I've a helper file helper.php where I keep some helper functions.
//helper.php
function isAuthLiked($authLikedPosts, $post)
{
return !! Auth::check() && $authLikedPosts->contains('id', $post->id);
}
Now in my test case, I wrote:
$this->assertTrue(isAuthLiked($authrenominations, $post[0]));
When I ran the test case, I get the error:
Fatal error: Call to undefined function isAuthLiked() in
C:\wamp\www\Nom7\tests\integration\UserTest.php on line 304
I've added the helper file in the compose.json auto-load. But the problem persists.
"autoload": {
"classmap": [
"database",
"app/Http/Controllers",
"app/Models"
],
"files":[
"app/helper.php"
],
"psr-4": {
"App\\": "app/",
"Acme\\": "app/Acme/"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
],
"files":[
"app/helper.php"
]
},
Check your path. You are using relative paths in your files array. You are running your tests from your tests directory, so cannot find app/helper.php
I have this composer config:
under classmap: "app/models"
under psr-4: "App\\Models\\": "app/models"
"autoload": {
"classmap": [
"database",
"app/models"
],
"psr-4": {
"App\\": "app/",
"App\\Models\\": "app/models"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
},
Here is my model:
namespace App\Models;
class TemplateRow extends Model{
protected $table = "template_rows";
}
in my controller i did: $row = new TemplateRow(); and I got class not found exception.
I did dump-autoload.
Thanks
You have to actually reference the model with it's namespace. You can either write:
$row = new \App\Models\TemplateRow();
Or add this before the class instead:
use App\Models\TemplateRow;
Also note that you shouldn't even have to add the entry under psr-4. If you're directory structure follows the namespacing. To be certain, call your folder Models and not models
I'm using Laravel for my project, I created a service provider and it's located in app/services/ToolboxServiceProvider.php, and I added
'providers' => array(
....
'services\ToolboxServiceProvider',
);
in my app.php config file. Now when loading the app, it says the service provider cannot find, I know that something's wrong with my path setting in that providers array, question is: how to make it right? Thanks in advance.
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"laravel/framework": "4.2.*"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "stable"
}
You should add into autoload => classmap section of your composer.json:
"app/services",
so it should look like:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/services",
]
},
In your provider file, at the beginning you should have:
<?php namespace services;
(in lower case).
And after those changes you should run:
composer dump-autoload
to rebuild classmap
Create the service provider using something like this in app/services folder, notice the namespace:
<?php
namespace Services;
use Illuminate\Support\ServiceProvider;
class ToolboxServiceProvider extends ServiceProvider
{
//...
}
Then in the providers array add 'Services\ToolboxServiceProvider'. Then add "app/services" in the classmap section in composer.json file and dump the autoloader.
What namespace is ToolboxServiceProvider using? Have you added autoloading in the composer.json file? Have you tried a composer dump-autoload?