Laravel 5 error Class 'App' not found in - php

I am currently learning laravel 5 and wanted to implement the repository concept.
as I understand. I should put an ioc.php and the config folder and put my bindings their
Here is my config/ioc.php
<?php
App::bind('QuestionRepository', 'IQuestionRepository');
App::bind('AnswerRepository', 'IAnswerRepository');
I get an error Class 'App' not found in

try prefixing App with \ like \App

For my case in lumen 5.4, undefined 'App' class issue solved by facade definition:
use Illuminate\Support\Facades\App;

In my case, my .env file contained a value with spaces after checking out a new project.
Running php artisan optimize for the first time would throw this error because my Exception handler was trying to use the '\App' global alias for the App facade. Commenting that code showed the real error;
"Dotenv values containing spaces must be surrounded by quotes."

Related

Getting error for artisan commands ( In routes-v7.php Call to undefined method Closure::__set_state() )

I have upgraded Laravel version from 5.7 to 8.34. I'm getting this error for all artisan commands.
To others: removing the routes-v7 might help, but it's possibly caused due to a return in your routes file (web.php). Remove it and use a controller to return a specific view/back() etc.
Call to undefined method Closure::__set_state()
In my case, I delete the bootstrap/cache/config.php file and run the PHP artisan vendor: publish but the same error occurs in the terminal.
Solution:-
1 Delete the routes-v7.php under the bootstrap folder path(bootstrap/cache/routes-v7.php) .
2 Run the PHP artisan optimize Command. then they recreate the deleted file in the repository.
Laravel 8 && Laravel 9
Please Support !!!!
https://stackoverflow.com/users/16749364/hitesh-sharma
#Tjab answer is the correct one. Just a few more thoughts:
It's because you're using Closures (a.k.a. Lambda functions function () use() {}) in your configuration. I'm pretty sure it's because you're using HTTP redirect routes. DON'T USE THAT FEATURE, since you won't be able to cache your routes, which is actually a good idea because it should increase your application speed. Otherwise your config can't be serialized (what caching actually means) to disk.
Instead create controller and action for each redirect.
Also check your configuration files for Closures and remove them (same for all other PHP frameworks).

Call to undefined function App\Providers\studly_case() after upgrade to Laravel 6.0.x

As I mentioned in the question, I just upgraded the Laravel version of my project from 5.8.* to 6.0.*.
Now I am getting the below error:
In AppServiceProvider.php line 32:
Call to undefined function App\Providers\studly_case()
Is Laravel supposed to throw error in it's own core file ? I was invoking the following command when I got this error, so I suppose this happens with any artisan command:
php artisan list
I saw in docs, that the new function is Str::studly from Str class file. Is it ok to change in the AppServiceProvider.php ?
Referring to the Laravel 6.x docs, I just discovered that the function for converting the string's current case to studly case is moved to Illuminate\Support\Str class, and named as studly().
So I added that dependency and then replaced the studly_case() to Str::studly() in AppServiceProvider.php, and then was able to run the artisan commands just like before.
I wasn't sure that changing AppServiceProvider.php is standard way or not, but it did worked so I am posting the answer here, that way it'll help anyone else facing the same issue.

laravel 5.3 aliases not found => undefined class

I created a project in laravel 5.2, and moving this to laravel 5.3
For this, I created a brand new laravel 5.3 project folder where I am copying views and controllers to the new project. When trying to get Auth working, I see a lot of "undefined class" errors.
By checking laravel documentation, I saw this, where I am stuck now
https://laravel.com/docs/5.3/authentication#retrieving-the-authenticated-user
so, in app.php I see
'Auth' => Illuminate\Support\Facades\Auth::class,
when in my controller, I get "Undefined class Auth" on this
use Auth;
this "undefined class" is solved by changing that line to
use Illuminate\Support\Facades\Auth;
first thing, why can't I just use "use Auth;" ?
In my controller file, I get "undefined class" error on DB, Auth and File
use DB;
use Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Http\Request;
use File;
in my app.php file, these names are aliased as
'App' => Illuminate\Support\Facades\App::class,
'DB' => Illuminate\Support\Facades\DB::class,
'Auth' => Illuminate\Support\Facades\Auth::class,
so I wonder what could be wrong with the aliases ?
Next to this, when trying to get the authenticated user, as described in the documentation, this line is enough
Auth::user()
Here, "user" throws this error:
Method 'user' not found in \Illuminate\Support\Facades\Auth
I checked around using google, comparing files in my Laravel 5.2 against my Laravel 5.3 folders, but did not see any solutions. Maybe someone here has the answer? It worked in my Laravel 5.2 project as described in the link above.
thank you
UPDATE
just try to rebuild the project by these commands in this order:
composer update
php artisan config:clear
composer dump-autoload
but still no progress

'There are no commands defined in the "baum" namespace' error when using "baum" for implementing Nested Set

For implement of a Nested Set for my news category I want to use Baum package.
After installing this Package via Composer and add BaumServiceProvider to config/app.php providers, I try to install a new Model named Category via Below command:
php artisan baum:install MODEL
But I faced to the following error :
[InvalidArgumentException]
There are no commands defined in the "baum" namespace.
Of course I ran php artisan command and But there were no command named Baum on generated commands list.
What should I do?
First you have to add the code below in config/app.php:
Baum\Providers\BaumServiceProvider::class
If the problem still persists configuration files may be cached. Just call:
php artisan config:clear
Make sure that you added
Baum\Providers\BaumServiceProvider::class
To the providers array in the app.php file located in config/app.php
Whenever I know that I have added something into Laravel's core files, and it's not working from the command line, I typically just run
php artisan optimize
to regenerate all of the classes, and it will typically work. Running this command is something that I find myself doing fairly often.
In AppServiceProvider.php (app\Providers) you can modify register function: public function register()
{
$this->app->register(\Baum\Providers\BaumServiceProvider::class);
}
Not sure why 5.2 have error, just tweak a bit myself.
Try add this on APP/Console/kernel.php
protected $commands = [
//Commands\Inspire::class,
// Register Baum nestedset command
Commands\BaumCommand::class,
Commands\InstallCommand::class
];
and create two files and copy BaumCommand.php and InstallCommand in /vendor/baum/baum/src/Baum/console.
then change a namespace at the top.
namespace Baum\Commands;
to
namespace App\Console\Commands;

Fatal error: Class 'PHPUnit_Framework_TestCase' not found in dbunit\PHPUnit\Extensions\Database\TestCase.php on line 24

I want to write tests for MySQL Database. When I try to extend the database extension, it gives me the above error in title:
class FixtureTestCase extends PHPUnit_Extensions_Database_TestCase
Note: I have installed PHPUnit globally and I can access it through phpunit in cmd. I have latest PHPUnit 4.5.1.
I also installed Dbunit globally through composer:
composer global require phpunit/dbunit
Any help will be much appreciated. I've used PHP 5.4.
For referring to classes that are located within the global namespace, you can simply prefix them with a backward ( \ ) slash.
Try with this:
class FixtureTestCase extends \PHPUnit_Extensions_Database_TestCase
With the leading backward ( \ ) slash, PHP knows that we are referring to the PHPUnit_Extensions_Database_TestCase class in the global namespace, and located that one.
More info here
Hope this help.
It just might be the version of DBUnit. For example I'm using PHPUnit 4.8 and it works with DBUnit 1.4.
I had the above error when I was using composer with "phpunit/dbunit": ">=1.2" and I ended up with DBUnit 2.0.
Test your DB how? Through an API you created or direct accesses to DB? i would recommend you to write an API to access your DB and then write the tests to the API.
for exemple if you want to insert a user you call your web-server address (localhost:3000) or something you have and then call /users through POST to insert and make the routes to that.
I would suggest using LARAVEL for that.

Categories