Laravel 5.5 not found class with correct namespace - laravel

My ./app folder looks like:
+-- app
+-- Classes
+-- Events
+-- EventBase.php
+-- EventX.php
There's nothing secret with EventX file:
<?
namespace App\Classes\Events;
class EventX {
// ...
}
EventBase.php represents a Facade that inside it I just try to instantiate an EventX:
public function someMethod() {
new \App\Classes\Events\EventX;
// ...
}
After this line, Framework throw an exception telling that class was not found:
Symfony\Component\Debug\Exception\FatalThrowableError (E_ERROR)
Class 'App\Classes\Events\EventX' not found
Even that:
file_exists(__DIR__ . '\EventX.php'); // true
I already had this issue before when trying to create Facades and solved by moving class file from his current directory and after moving back (yeah, I don't why but it worked).
Something tells me that this is an issue of autoload process, so I tried these command (but still not working):
php artisan cache:clear
php artisan clear-compiled
php artisan config:clear
composer dump-autoload
What can I do in order to investigate the problem?

I think the problem is with the php tag <?
<?php
namespace App\Classes\Events;
class EventX {
// ...
}
PHP also allows for short open tag <? (which is discouraged since it is only available if enabled using the short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option).
Link

You need to add the new namespace to your composer.json to the key "psr-4"
"psr-4": {
"App\\": "app/",
"Classes\\": "app/classes/"
otherwise composer can't detect your new namespace
Another approach can be found here:
https://stackoverflow.com/a/28360186/6111545

Related

Run Laravel Seeder From Package

I'm working on a package for an internal Laravel application and I'm having trouble running a seeder that exists in the package directory.
In my package's composer.json file located in packages/vendor/packagename, I've added the following:
"autoload": {
"psr-4": {
"Vendor\\PackageName\\": "src/",
"Vendor\\PackageName\\Database\\Factories\\": "database/factories/",
"Vendor\\PackageName\\Database\\Seeders\\": "database/seeders/"
}
},
I have the following file located in "packages/vendor/packagename/database/seeders/DepartmentSeeder.php"
<?php
namespace Vendor\PackageName\Database\Seeders;
use Illuminate\Database\Seeder;
use Vendor\PackageName\Models\Department;
class DepartmentSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Department::factory()->count(10)->create();
}
}
I then attempt to run the following command:
$ php artisan db:seed --class="Vendor\\PackageName\\Database\\Seeders\\DepartmentSeeder"
Target class [Vendor\PackageName\Database\Seeders\DepartmentSeeder] does not exist.
If I move the seeders directory into my src directory and run the following, it works, but I'd rather keep my seeders in my database directory.
$ php artisan db:seed --class="Vendor\\PackageName\\seeders\\DepartmentSeeder"
Does anyone have any idea why the class isn't being found? All my Google search results are purple and I even went to page two =/
Solution
In my particular case, composer dump-autoload wasn't doing the trick. What I ended up doing was running composer update vendor/packagename and whatever the issue was, it was resolved.
Hopefully, this helps anyone else who may have similar issues.
In my particular case, composer dump-autoload wasn't doing the trick. What I ended up doing was running composer update vendor/packagename and whatever the issue was, it was resolved.
Hopefully, this helps anyone else who may have similar issues.

Service Provider not booting in Laravel 5.8

I've created a new service provider to observe a model (App\Providers\EloquentEventServiceProvider.php), like so:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Staff;
use App\Oberservers\StaffObserver;
class EloquentEventServiceProvider extends ServiceProvider
{
public function boot()
{
Staff::observe(StaffObserver::class);
}
}
I've also added it to the config file (config\app.php):
return [
...
'providers' => [
...
App\Providers\EloquentEventServiceProvider::class,
...
]
...
]
The observer methods aren't working though. If I move Staff::observe(StaffObserver::class); to the AppServiceProvider class, it works just fine. So clearly this is an issue with getting my service provider to boot. I've tried php artisan config:clear, php artisan clear-compiled, composer update and composer dump but none have work. Any help is greatly aprpeciated.
your Oberservers name is wrong, as mentioned in the laravel doc observers laravel doc it should be Observers which means all of your observers should be within App\Observers instead of App\Oberservers.
so here we have 2 solutions :
1- if you want to keep the namespace App\Oberservers, you should run these 2 commands below because autoloading of the files may not work properly because we created a new folder Oberservers:
# Autoloading of files
composer dump
# Configure the cache
php artisan config:cache
2- the second solution is to just rename your actual Oberservers folder to Observers, in that way the autoloading of files will work well.

Facing problem while trying to insert data in database, LARAVEL

I am getting this error "Class 'App\Models\Student' not found".
Please check my codes:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Student;
class StudentController extends Controller
{
public function data(){
$stud = new Student;
$stud->name = 'Mona Lisa';
$stud->rollnumber = '001';
$stud->save();
}
}
Your error is most likely in the namespace declaration of the Student model.
check in your class file App\Models\Student.php that the namespace is correct.
namespace App\Models;
then try to run in the console:
composer dump-autoload
Change this line:
use App\Models\Student;
to:
use App\Student;
Your error suggests that class is not loaded into the controller. I am guessing from the issue that you have created a separate directory for models if you have then you need to update your composer file to include new class paths for model classes. locate autoload in you composer.json file and add an entery in classmap array as shown below
"autoload": {
"classmap": [
"database",
"app/models"
]
}
then on command line navigate to your project directory and run the following command:
composer dump-autoload
and if you didn't create new models directory then just run
composer dump-autoload
Make sure your namespace of student model
use App\Models\Student If your model is in App\Models directory
use App\Student if your model is in app directory
Finally run composer dump-autoload
check your namespace of student model
use App\Models\Student If your model is in use App\Models directory
use App\Student if your model is in app directory
then go to command line and run composer dump-autoload

After rename app folder and namespace artisan make shows Exception: Unable to detect application namespace

I've renamed my app folder to aplicativo and changed the namespace to aplicativo. Everything works but artisan make commands.
I changed namespace through composer.json
"psr-4": {
"aplicativo\\": "aplicativo/",
}
Plus command:
artisan app:name aplicativo
You won't get this to work. You can rename the namespace (as you did with the command), but you can't rename the directory because it's hardcoded as app.
You can see the source here.
Then... if your using composer try this command
composer dump-autoload
The good news is that Laravel has now added support for custom App path. You can override the App path by adding the following code in bootstrap/app.php (or provider).
/*...*/
$app = new Illuminate\Foundation\Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
// override the app path since we move the app folder to somewhere else
$app->useAppPath($app->basePath('src'));
Similarly, you can change other paths (database, language, storage, etc.). You can find all the available methods here.

Class 'Tests\DuskTestCase' not found in ExampleTest.php

I am trying to run Laravel Dusk tests, but when I run the test, a new Chrome tab pops up with this message.
Fatal error: Class 'Tests\DuskTestCase' not found in path-to-project\tests\Browser\ExampleTest.php on line 9
All I have done so far is run composer require --dev laravel/dusk:^1.0 and php artisan dusk:install.
This is my ExampleTest.php (exactly how Laravel set it up)
<?php
namespace Tests\Browser;
use Laravel\Dusk\Chrome;
use Tests\DuskTestCase;
use Laravel\Dusk\DuskServiceProvider;
class ExampleTest extends DuskTestCase
{
/**
* A basic browser test example.
*
* #return void
*/
public function testBasicExample()
{
$this->browse(function ($browser) {
$browser->visit('/')
->assertSee('Laravel');
});
}
}
DuskTestCase.php is also just as Laravel set it up and has the namespace namespace Tests;.
I am using Laravel 5.4 and Dusk 1.0. I am running the test through PhpStorm, using the work around described here.
Anyone know why DuskTestCase can't seem to be found, even though it appears to be set up correctly? Thanks in advance.
In composer.json:
add "Tests\\": "tests/" in
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Tests\\": "tests/"
}
},
then, run composer dump-autoload to reload your packages.
I had this error due to using out-of-date docs that didn't include this line:
$ php artisan dusk:install
If composer dump-autoload does not solve problem, you can try these steps.
Visit homepage in your browser and check if it renders properly. If not, then you probably have a problem with your webserver configuration (Hint: isn't your project subdirectory of document root?).
You can try Laravel inbuilt server via php artisan serve. If homepage is accessible in your browser now, then you can try dusk again.
In that case, remember to update your .env file to match APP_URL=http://127.0.0.1:8000,
and run php artisan dusk from another cli window, cause php artisan serve needs to be running also.
if you test using a phpstrom then u have set path of phpunit ......
in settings/languages & framework/php/test frameworks and use composer autoloader path and then select a path of your laravel dusk project with autoload.php file.....
set file a vendor/autoload.php file in path to script...

Resources