Linking a new Helper File in CakePhp 3 via Composer - php

I am using Cake PHP 3, and under the src folder, I created a new folder called Helper and created a new file inside it. And now I went to composer.json and added the required file key inside the autoload. You can see it below:
"autoload": {
"psr-4": {
"App\\": "src"
},
"files": [
"./src/Helper/general_helper.php"
]
}
But after, doing composer dump-autoload, getting require(/opt/lampp/htdocs/project_name/vendor/composer/../../src/Helper/general_helper.php):
Its not under the vendor folder, I think that's why its giving the error. Any idea, how to solve this issue, cause I want to maintain a separate folder. Thanks.

Related

Composer class not found even if it exists

I'm developing a Laravel package but I have a problem with composer autoloading.
My package has 2 folders under src folder. One of them is named Laravel and the other one is Telegram. Here is the package structure:
./packages
.../typhoon
...../src
......./Laravel
........./Providers
............LumenServiceProvider.php
............LaravelServiceProvider.php
......./Telegram
..........Api.php
.....composer.json
This package is developed under SaliBhdr/Typhoon namespace.
I have added the packages/typhoon/src directory in Laravel's composer file like so:
"autoload": {
"psr-4": {
"App\\": "app/",
"SaliBhdr\\Typhoon\\" : "packages/typhoon/src/"
}
},
And add the src/ address in package composer.json file like so:
"autoload": {
"psr-4": {
"SaliBhdr\\Typhoon\\": "src/"
}
},
Here is the strange behavior begins. When I execute the php artisan serve command Laravel throws an error that says :
Class 'Salibhdr\Typhoon\Laravel\Providers\LumenServiceProvider' not found
And if I check if the class exists with class_exists('Salibhdr\Typhoon\Laravel\Providers\LumenServiceProvider') function it returns false. But if I check if Salibhdr\Typhoon\Telegram\Api exists it returns true.
I checked the autoload_classmap file and notice that the composer includes all the classes under Telegram subfolder but not Laravel subfolder.
Why composer acts weird like this? why did it include one subfolder and not the other? It is something that I do every day and never seen anything like this.
I desperately need help. Any help would be appreciated
You are trying to initialize Salibhdr\Typhoon\Laravel\Providers\LumenServiceProvider but in your composer it's "SaliBhdr\\Typhoon\\": "src/".
Notice the capital B in your composer. PHP classes are case sensitive so you have to make sure it's either both lowercase or both uppercase.
Also make sure to run composer dumpautoload if you modify composer.json.

ErrorException (E_ERROR) Class 'app\MyLib\MyClass' not found - Similar class working fine

I am creating a web application. I am relatively new to Laravel, and still learning. Anyway, here is my problem:
I am trying to include custom class into my project. I managed to include one class - Administrator, and tried to do the same with the other - Profesor, but despite the fact that first is working fine, the other does not.
I created files inside my app folder, subfolder Biblioteka, named Administrator and Profesor.
Inside each file I put:
namespace app\Biblioteka;
Inside composer.json I made following alterations:
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories",
"app/Biblioteka/Administrator",
"app/Biblioteka/Profesor"
],
"files": ["app/Biblioteka/Administrator.php",
"app/Biblioteka/Profesor.php"]
},
After that I run:
php artisan route:clear
php artisan cashe:clear
composer dump-autoload
and got the following message:
[RuntimeException]
Could not scan for classes inside "app/Biblioteka/Administrator"
which does not appear to be a file nor a folder
dump-autoload [--no-scripts][-o|--optimize][-a|--classmap-
authoritative][--apcu][--no-dev]
I included:
use app\Biblioteka\Administrator;
use app\Biblioteka\Profesor;
in files using mentioned classes.
Application is working for class Administrator, but not for class Profesor.
Class Administrator was created first. After everything started working properly I made class Profesor the same way as I did with Administrator. It reports the error at the line where constructor for the class is called. I cannot figure out why the second class is not working properly.
Thanks to anyone who can help me figure this one out :)
There's no need to add to the classmap and files blocks if you're putting your custom classes in the app directory. Remove those lines, then just do use App/Biblioteka/Administrator; in your PHP files.
This bit:
"psr-4": {
"App\\": "app/"
},
means that anything in the app directory can be referenced via the App namespace. Thus, for app/Foo/Bar.php, the class is referenced as App\Foo\Bar.

Laravel 5: add custom library in sub folder of "Libraries" folder

I need to add a custom library to Laravel 5, but I want to add in a sub folder of a "Libraries" folder.
I mean, I have "Libraries" folder inside "app" folder, and I would like to add another folder inside "Libraries" folder and put a class inside it.
What I've done is:
Created "Libraries" folder inside "app" folder;
Created "FusionChartsWrapper" folder inside "Libraries" folder;
Created "FusionCharts.php" file inside "FusionChartsWrapper" folder.
The FusionCharts class has the correct namespace:
namespace App\Libraries\FusionChartsWrapper;
but I can not use it, cause I get this Laravel error:
Class 'App\Libraries\FusionChartsWrapper\FusionCharts' not found
If I move the class inside the "Libraries" folder, it works.
Any idea?
The best way is to add the whole folder to autoload in composer.json file
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/Libraries" // =>folder you want to add
],
"psr-4": {
"App\\": "app/"
},
},
then run composer dump-autoload
Now every files in the Libraries folder can be access every where
and no need to use
namespace App\Libraries\FusionChartsWrapper;
just add \ before function you want to use directly

Autoload folder without changing composer.json file - Laravel 5.4

I've created a package that creates a folder in the root of the project.
Creating it in the app folder is for me not clean enough. Cause I don't want it to look like it's merged with the laravel framework.
This package is for our company and will be used a lot.
So instead of changing the composer.json file everytime to add the folder to the autoloader I'm trying to just autoload it from the package.
Is something like that possible and how?
Are you saying that you do not want to add it in your composer.json file here?
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Company\\": "company/"
}
},
That is what I would do.
What if you just use the folder structure as the autoloader namespace? That should work. For example:
<?php
use Company\Foo;
new Bar();
Where you would have a folder called company/Foo with all the classes inside declaring their namespace like so:
<?php
namespace Company\Foo;
class Bar {
//
}

Laravel 5 - Change Models,Views,Controllers path

i have edited psr-4 on composer.json
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"Marka\\Urun\\": "vendor/Marka/Urun/src/",
"App\\": "app/"
},
"files": [
"vendor/Marka/Urun/src/helpers.php"
]
},
I want to change file(routes.php, helpers.php and Models,Views,Controllers) pathes to :
Vendor/Marka/Urun/
How can I do it ?
You're trying to get some modular structure if I get it right.
If so, instead of trying to set a different namespacing from composer for each of your modules under vendor directory; i think you may try to use something like http://sky.pingpong-labs.com/docs/2.0/modules
Otherwise as you may know, by using PSR-4 and directory structure, if you coded your files properly all necessary files would be loaded automatically as you named (namespaced) them.
By the way, just in case you didn't know you may also need a ServiceProvider to boot everything up for Laravel on your package.
I also suggest you to read https://laravel.com/docs/5.2/packages if you need any help about development structure/functionality.

Categories