Laravel 9.3, reorganize request classes structure - laravel

I have tried to reorganize my request classes. For example, I tried the following:
Instead of: use app\Http\Requests\UpdatePasswordRequest; (in the folder app\Requests.
I wanted: use app\Http\Requests\Account\Password\UpdatePasswordRequest: (in the folder app\Http\Requests\Account\Password\
At the: composer dump-auto I get a message like this:
The class App\Http\Requests\UpdatePasswordRequest in E:/htdocs/project/app\Http\Requests\Account\Password\UpdatePasswordRequest.php does not conform to the psr-4 autoloading standard. Skip.
Is there a workaround for this, I want to organize my request files for clarity.

Your issue is that you have put .php in the namespace of the file so simple changing the namespace of the file to
namespace Http\Requests\Account\Password;
class UpdatePasswordRequest extends FormRequest
This should do it!

Related

how laravel handle illuminate namespace

As far as I know, namespace is used for preventing class, function, ... collision in PHP. In Laravel 8 when we create a model for example, the name space is App\Models and when we want to use this model, we have to use it like use App\Models\MyModel. My question is when we use use Illuminate\Support\Arr, why we don't use the full path explicitly vendor\laravel\framework\......
It’s part of the PSR-4 auto loading standard which replaces the PSR-0 auto loading standard. Composer compiled an autoload.php from your project’s and dependencies’ composer.json. As part of the bootstrapping process that file is included in the application and registers each namespace.

Laravel 5.7, Moved User class, RegisterController can't find it

I am planning a somewhat complex Laravel application, and so I have moved the default User.php class from App\User to App\Http\Models\Users\User.
Paths & Code
I have changed the paths appropriately in the code (please see screenshot).
Unfortunately, I am still getting this error:
error message
I have not changed composer.json, nor any other files than config/auth.php and app/Http/Controllers/RegisterController.php. Also, I am unable to locate any other files in the project that are using App\User (other than RegisterController).
I have updated and migrated the file for the users table.
I'm probably missing something obvious, so please be kind. ;-)
Can someone point out what I'm missing?
The path must coincide with the namespace to the file be loaded as expected.
So if your path is
app\Http\Models\Users\User.php
Your class namespace must be:
namespace App\Http\Models\Users;
Note that the App on the namespace must be in CamelCase. Otherwise it will not work.
only 4 files contains App\User class reference
app/Http/Controllers/Auth/RegisterController
config/app.php
config/services.php
database/factories/UserFactory.php
The namespace in my User class was incorrect (missing 'Http'):
Wrong: namespace App\Models\Users
Right: namespace App\Http\Models\Users
Thank you all for your suggestions, especially #elias-soares!

Laravel 5 namespaces

I just downloaded Laravel 5 and started migrating to it. However, I find the required use of namespaces really annoying.
I don't feel like I am getting much from it, other than cluttering my code.
How can I disable the namespacing requirement?
I don't think you should disable or remove namespaces. The main reason for namespacing is to avoid conflicts with classes that have the same name. As soon as an application gets larger you will have classes that have the same name. Example from the Framework source:
Illuminate\Console\Application and Illuminate\Foundation\Application
Both are called the same. Only because of the namespacing you can import the right class. Of course you could also name them:
ConsoleApplication and FoundationApplication
But while the namespace normally is only used when importing a class at the top of a file:
use `Illuminate\Console\Application`
The name itself is used everywhere in the code. That's something that really clutters up your code, too long class names.
Besides the naming thing, namespaces also encourage better structure and help with knowing where your files are. That's because Laravel's default structure is PSR-4 compliant. That means if you have a controller App\Http\Controllers\HomeController you can be certain that you will find a HomeController.php under app/Http/Controllers.
I am aware of that, but it's not needed in the project I am working on.
Maybe it doesn't make sense for the current project but getting used to namespaces will help you tackle bigger projects in the future
And being a Sublime Text user, which doesn't have autoimport, it really gets to be a pain
I don't know Sublime Text that well, but CodeIntel might have auto import. Otherwise consider switching to another editor / IDE. I can highly recommend JetBrains PhpStorm
In the end, if you still don't want to use namespaces, keep using Laravel 4 or search for another framework that follows less good practices...
Removing namespaces from your app classes
While a totally don't recommend this, it is possible to at least remove some of the namespacing in your application.
For example the default controller namespace App\Http\Controllers can be changed to no namespace at all in RouteServiceProvider:
protected $namespace = '';
And for your models you can just remove the namespace in the file and your good. But keep in mind that without namespaces PSR-4 autoloading won't work anymore. You will have to autoload your files using classmap in composer.json
You can avoid using namespaces for own classes by defining them in the global namespace in your composer.json file. Like this:
"autoload": {
"psr-0": {
"": ["app/Http/Controllers/",
"app/models/",
"app/helpers"
]
},
You will also have to change your app/Providers/RouteServiceProvider.php to:
protected $namespace = '';
for routing to work.

Why you need to manually register classes via composer in in Laravel?

I come from a CodeIgniter background and everytime I create a new controller, I don't have to register it by calling a program like composer.
Why this is required in Laravel?
This is because Laravel uses the "Classmap" autoloading mode in Composer. With this, it scans the specified directories and compiles a class map. Because of this, you need to run composer dump-autoload when you add a new class, so that it can update the compiled class map.
However, it is fairly easy to use PSR-4 autoloading with Laravel. This autoloading standard requires you to follow stricter namespacing rules with your class names and filenames.
To use PSR-4, you'll need to namespace your controllers, models, etc. like this:
Yourname\Yourproject\Controllers
Yourname\Yourprject\Models
etc. You'll also need to change the controllers, models, etc. directories to be capitalized - Controllers, Models, etc., - to match the namespaces. Then, add this to your composer.json file:
"autoload": {
"psr-4": {
"Yourname\\Yourproject\\": "project_dir/app/"
}
}
This will map the Yourname\Yourproject namespace to the app directory. So, for example, to find the class Yourname\Yourproject\Controllers\FooController, Composer will automatically know to look in app/Controllers/FooController.php. At the top of FooController.php, write
<?php
namespace Yourname\Yourproject\Controllers
class FooController extends \Controller {
...
(Note: It's not required to namespace everything with Yourname\Yourproject\; it's recommended, but a simple Yourproject\ will work as well.
You can still also add an autoload.classmap section to your composer.json for classes that don't follow PSR-4, but you'll need to run composer dump-autoload for them.
For more information on the various autoloading modes in Composer, see here.

Composer autoload with our naming convention

Our naming convention for classes has been name_class.php. Is there a way to append _class in the class file name look up? I changed the loadClass function in the ClassLoader.php file to get it to work but a composer install overwrites my change.
No, composer does only support PSR-0, PSR-4 and for all other schemes the classmap.
If your name scheme does not conform to PSR-0 at least, all you can do is to use the classmap and always create a classmap update if you add a new class. I'd highly recommend to create new classes conforming to PSR-4 or PSR-0 standard to get rid of this need when using Composer. The old classes might still be located somewhere and loaded via classmap if you do not want to rename the files.
You can however run the composer autoloader parallel to your own. Then you are still responsible for your own classes, but use Composer for all the libraries.

Resources