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

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!

Related

Laravel 9.3, reorganize request classes structure

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!

Phpstan Class_parent not found

I am trying to get static PHP code analysis to work on my OXID 6 eshop.
I am, however, hitting a brick wall when it comes to this:
When I run vendor/bin/phpstan analyse --configuration phpstan.neon source/modules/myvendor/mymodule I always get errors like Class mynamespace\Article_parent not found.
This is probably because in Oxid, the eshop classes are not directly overwritten, but instead declared like this
<?php
namespace mynamespace;
class Article extends Article_parent
{
...
and in a metadata.php the overwritten class is defined:
'extend' => [
\OxidEsales\Eshop\Application\Model\Article::class => \mynamespace\Article::class,
],
Phpstan does not seem to be able to resolve this, I've tried many different configuration files, like this one
parameters:
level: max
inferPrivatePropertyTypeFromConstructor: true
autoload_files:
- vendor/oxid-esales/oxideshop-ce/source/oxfunctions.php
- vendor/oxid-esales/oxideshop-ce/source/overridablefunctions.php
or this one
parameters:
level: 7
autoload_files:
- vendor/oxid-esales/testing-library/base.php
- vendor/oxid-esales/oxideshop-ce/source/oxfunctions.php
- vendor/autoload.php
but to no avail.
How can I get phpstan to work?
Please upgrade to the latest PHPStan version 0.12.33.
In version 0.12.26 a lot has been reworked that's described in this article. The main takeaway is that you should no longer get "class not found" for classes that clearly exist without any need for additional configuration.
Also, autoload_files has been deprecated. Please follow Discovering Symbols guide to see what to do instead (you can most likely just delete this section from your configuration).
The \mynamespace\Article_parent class does not exist and will be created at runtime as an alias. This is because it depends on your configuration / other modules installed where this alias points to.
My colleague Alfred Bez created the oxid-dump-autoload tool, what will dump out the module chain based on your current configuration and setup, so that PHPStan / Psalm can find the classes:
https://github.com/alfredbez/oxid-dump-autoload
Hope I could help

I can't seem to access my Auth inside a vendor package

I had a Laravel app that I created, then I added another Laravel package for Oauth2 for a CRM.
This created a vendor and migration for DB. I tested it and it worked, I authenticated my CRM.
Now I would like to have that record be tied to the user_id in the Auth scaffold that I created with composer make:auth
However, when I try to get the current user from inside of those vendor files, it is not pulling in that information and is giving me errors.
I think this may be a namespace issue, but maybe it could be a guard or middleware. I am not sure. I am pretty laravel
I have tried a handful of other solutions, but I wasn't sure if those were tied to my specific issue
<?php
namespace Djaxho\LaravelInfusionsoftOauth2\Http\Controllers;
//namespace App\Http\Controllers\Auth;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Djaxho\LaravelInfusionsoftOauth2\Infusionsoft;
use Illuminate\Support\ServiceProvider;
//use Illuminate\Support\Facades\Auth;
class AuthorizeInfusionsoftApiController extends BaseController
'''
print Auth::user()->id;
I see that it is in its own namespace, is this what is the issue, laravel is not looking outside?
I get this error:
Class 'Djaxho\LaravelInfusionsoftOauth2\Http\Controllers\Auth' not
found
Their page says it pretty straightforward on what to do.
https://packagist.org/packages/djaxho/laravel-infusionsoft-oauth2
Installation (These instructions are for an 'alerady set-up' laravel
project with a functioning database set up) Add the service provider
for laravel by adding the following line to your 'providers' array in
the file congig/app.php
Djaxho\LaravelInfusionsoftOauth2\LaravelInfusionsoftOauth2ServiceProvider::class,
And add
use Djaxho\LaravelInfusionsoftOauth2\Infusionsoft;
to your model where you are calling the auth from.
That being said, I dont know if this package handles auth seperately or doesnt. In that case you should use the laravel facade,
use Illuminate\Support\Facades\Auth
You need to give you routes in your package web middleware and it will works fine
.

Laravel dosen't see path to file with Custom Request Class, how to fix it?

I created Custom ReadRequest Class. Than I created folder with "Order" name in Requests folder, and put there my ReadRequest.php file:
After this manipulation Laravel doesn't see this file in my Controller:
How can I fix that?
You haven't written how you know that Laravel doesn't see this file but looking at your screen it seems your IDE doesn't see it it either. So make sure in your ReadRequest.php file you have
<?php
namespace App\Http\Requests\Order;
class ReadRequest
{
// ...
}
It's very possible that you have invalid classname in this file or invalid namespace, so your IDE shows you the error and also composer cannot load the file.

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.

Resources