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 {
//
}
Related
I have a php file with Paysafecard class. File is called Paysafecard.php. I'd like to include it in my controller. How can I do it?
If you are using Symfony surely you are using the composer autoload.
Go to the root directory path of your project and search the composer.json file.
Within it you find a directive like this :
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
This sentence indicates the mapping between the name and the directory.
Now look at the actual file path, for example /src/lib/Paymenterio.php
Go to the controller where you want to use the class replace the path with the autoload alias and remove the extension.
use App\lib\Paymenterio;
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.
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
Here is my current directory:
As you see, I've made a new folder named classes in the app folder. Also I have some .php files (contains some classes) in classes folder. But I cannot access those classes in my controller. Noted that I add them to my controller like this:
use app\classes\Myclassname;
But still My classname won't be recognized and Laravel trows this:
Class 'app\classes\Profile' not found
How can I fix mentioned problem?
Add namespace into your class file, created class file like app/classes/MyclassnameXXX.php
namespace App\classes;
class MyclassnameXXX {}
Now, you have a right to access this class by using the namespace within your classes like:
use App\classes\MyclassnameXXX;
Hope this work for you!
You can autoload classes using composer like this.
"autoload": {
"classmap": [
"database",
"app/classes"
],
"psr-4": {
"App\\": "app/"
}
},
Don't forget to run composer dump-autoload to reload classes.
Also, add namespaces to your classes.
namespace App\classes;
class MyClass {
}
Or, you could use fancy namespaces like Laravel using psr-4 autoloading like this
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"App\\Classes": "app/classes"
}
},
I'm working with Laravel framework.
I have a question regarding classes creation.
To explain my question, i will give you an example:
class articleController extends Controller{
public function bla(){
$a = new MyNewClass($colorfulVariable);
$b = $a->createSomething(new MySecondNewClass());
}
}
Now, the classes MyNewClass and MySecondNewClass do not exist, I want to create them, use them in every controller I want, just like I use the Redirect or Request classes that Laravel offers.
How can i create this classes? Make a new functions to use them in my laravel project?
you can just create those classes in your app folder (or in subfolder for example we create a Libraries folder with all the utilities). Just include a use App\YourClass; before using those classes and laravel will include the classes for you
Laravel 5.* is autoloaded using PSR-4, within the app/ directory. You can see this in the composer.json file.
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
Basically, you could create another directory within app, and then namespace your files in there as appropriate:
app/folder1/folder2/SomeClass.php.
Then, within your SomeClass.php, make sure you namespace it:
<?php namespace App\folder1\folder2;
class Someclass {}
Now, you can access this class using the namespace within your classes:
use App\folder1\folder2\SomeClass;
You can create your classes as usual, but don't forget to include them into composer.json autoload section:
"autoload": {
"classmap": [
"App\MyClassesNamespace"
....
And then run composer dumpauto command to rebuild autoloader file. If you'll not do this, you'll not be able to use your classes in your application.
You don't have to do anything other than below things:
Create a class in App\ folder.
Whenever you use the class, you will have to import the class using "use App\XYZ;" on top of the importing class.
thats it. Mostly people follow convention and then create "Service" folder on App\ folder. It is good to create alien class into service folder which are other than laravel framework classes.