I have learned that in best case a namespace should represent the path of its file. That means if the file is into the subdirectory "models", like in this case
vehicles
cars
models
the namespace should be: namespace vehicles\cars\models;
Now, I have an example from Symfony 3 that looks like this:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
I would like to understand where those namespaces are located in. "Symfony" and "Sensio" I can both find in the directory "vendor". For example if I follow the path "Symfony", I don't find "Bundle" or if I follow "Sensio", there is also no folder called "Bundle".
Is that because Symfony doesn't care about the standards or am I just not understanding how it works? I hope it's the second one.
I hope someone can explain that to me, so I can understand.
Symfony has a more complex file structure the namespace Symfony corresponds to the symfony/symfony/src/Symfony folder so if you are looking for Symfony\Bundle\FrameworkBundle\Controller\Controller just go there and then you will find Bundle/FrameworkBundle.
On the other hand, for most vendor bundles the namespace corresponds to the exact folder structure like for example Sensio\Bundle\FrameworkExtraBundle\Configuration\Route
If you really can't find the path to a vendor bundle you can always try looking into vendor/composer/autoload_namespaces.php and autoload_psr4.php.This is where composer defines the correspondences between directories and namespaces for your project
Hope that helps
Related
What I've done
I used the command php artisan make:model User without mentioning model folder as the command should have been php artisan make:model Models\User.
I am an amateur in terms of laravel and just started php and laravel last month and made a whole multi vendor project around it without the models being in models folder.
What I want
Now, I'm just trying to structure my code in a cleaner manner and facing errors as have to change the path of models wherever it is mentioned. I tried doing them but still can't get rid of the errors.
Latest version of Laravel, by default, file is already created inside the Models folder. If you have a model outside folder and what you want is to incorporate it there, the best thing to do is to change the built-in namespace at the top of everything. For example:
namespace App\Models;
If you want is to completely change the path, first create the model and then move folder manually. Finally, in namespace mention it:
namespace FolderA\FolderB\FolderC;.
Although it is always better to follow the default Laravel structure, is cleaner.
You need to modify your
Project/config/auth.php
file on
'model'=> AppName\Models\User::class,
The best thing to do is to move 1 model at a time and replace all of its import namespaces. Test your application if everything works and then continue with the next model and so on.
Example:
Move User.php from App\ to App\Models folder
Update the namespace of User.php from namespace App; to namespace App\Models;
Find and replace in your project (global search) for: use App\User.php and replace all results with use App\Models\User.php
Check your application and run tests if you have them.
If everything is ok, you can continue moving the next model to the Model/ folder and repeat step 1 to 5
For the model User.php: As #saeed mentioned, update your config/auth.php if needed.
Note that IDE's these days can do the most work for you, like PhpStorm can do all the find & replaces for you when you pressing F6 on the User.php file in the tree and update its path.
I am seeing namespace errors all over the place and I am not sure why.
For exampe I have:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
and Configuration is in red and the error reads:
Undefined namespace Configuration less... (Ctrl+F1)
Referenced namespace is not found.
How do I fix this?
Since you say "Configuration is in red" I assume the problem you are experiencing is related to the IDE
If you are using PHPStorm there's a specific Symfony plugin that handle all the namespacing pretty much automatically, as long as your setup is standard.
By standard I mean a project dir containing the app/ src/ web/ ... dirs (if you're using the 2.x Symfony dir layout)
This link will tell you everything you need, in great detail
It may be your .idea folder which is corrupted. Close your project, delete the .idea folder and restart the IDE. It worked for me.
atm i'm beginning to develop a big application and the current structure of laravel just doesn't fit what i had in thought. The controllers, models and views are all in seperate folders just bunched up and things could get messy when there's loads of them.
So I was hoping I could change the default way laravel loads it's controllers views and models. My approach would be something like this:
-App
--Content
---Login
----Controller
----Model
----View
----Js
---Home
----Controller
----Model
----View
----Js
--BaseContent
---BaseLayout
---BaseController
---BaseJS
So when the route is home the controller model view and javascript all get bundled in a folder instead of having all of it in one controller directory, model directory, view directory etc.
So my question is does someone know how I could change the way laravel load's it's dependency's? I'm kinda hoping for a configuration file I just can't seem to find.
I understand your thoughts. You want to group by feature.
It's possible. All you need are namespaces conform PSR-4. In composer.json you can link/specify the namespaces and paths, for example, you have in your controller this line:
namespace App\Http\Controllers;
In composer.json see the following fragment in autoload:
"psr-4": {
"App\\": "app/"
}
The namespace App refers to the directory app, and all classes with the namespace beginning with App\ will be autoloaded. So it's simple for your to change the directory structure.
I am currently running Slim and Twig, my folder structure is as follow
/application/modules
In my composer.json I have
"autoload": {
"psr-4": {
"Application\\": "application/modules/",
"Core\\": "application/",
"Middleware\\": "application/slim/middleware/"
}
}
My issue is is that in Application\modules\ I have a directory for each module. Now my question is, is it possible to make composer iterate through the sub directories when using PSR-4 autoload?
I see an issue with your PSR-4 declaration.
You shouldn't place "Core" classes inside a folder that has subfolders for other namespaces.
PSR-4 autoloading in Composer works like this: If the fully qualified class name of the class to load matches a prefix declared in any of the PSR-4 definitions, then the prefix mentioned in the prefix is removed from the class name, and the remaining class name is mapped to a filename and being searched.
If you have classes named Application\ in the folder application/modules, and you have classes named Core\ in the folder application, then technically Composer might find files that match a class name like Core\modules\Whatever, although these file will contain a class Application\Whatever instead.
I recommend moving all the Core classes into their own folder and point to this in the PSR-4 declaration.
The problem with your original question is that you omit an important information: What is the class and file structure for your modules?
Composer's autoloader will happily resolve any class that starts with the namespace prefix Application, remove that prefix from the classname, convert the remainders into a path name and search for that file in application/modules/. Given that you have a module class Application\MyModule\Foobar, it will be searched in application/modules/MyModule/Foobar.php. Why? Because the prefix Application will be removed to allow for shorter path names in PSR-4. (Using PSR-0 would mean you have to have a folder for every level of namespace in the classname.)
Note that it is recommended for optimum performance to make the prefix for namespaces as long as possible, because Composer allows to have more than one directory for any given prefix, but then has to search for the correct file in more than one directory. Searching for files is time consuming!
Instead of Using composer I download zip file of a bundle and extract this bundle to my symfony project. I change all relative address for example I change all namespace Trsteel\CkeditorBundle; to namespace Acme\TrsteelCkeditorBundle; for doing this I searched the downloaded directory for every Trsteel\CkeditorBundle and change all of them to Acme\TrsteelCkeditorBundle now when I want to use this Bundle on my project I register this bundle to my appKernel.php of symfony project But I reached this error
Error: Class 'Acme\TrsteelCkeditorBundle\TrsteelCkeditorBundle' not found in /Users/kingkong/Documents/workspace/dev/Project/SRC/app/AppKernel.php line 45
File names should be the same as namespace structure, capitalization may be an issue, so check that.
You may want to place the bundle in this way - "Acme\Trsteel\CkeditorBundle\CkeditorBundle"
Check whether you have actually changed the folder structure from Trsteel\CkeditorBundle to Acme\TrsteelCkeditorBundle. And also check your routing files as well. (I assume you have done the routing part as the error won't say that it can't get to that location.)
Cheers!