I have a folder named "Models" located at app/models in my laravel project directory.
I want to use all models by a single line of code in controller, instead of using each one like:
use App/Models/User;
use App/Models/Roles;
You could try this
spl_autoload_register(function($className) {
include_once $_SERVER['DOCUMENT_ROOT'] . '/app/models/' . $className . '.php';
});
You need to add this folder in your composer.json file.
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/Models"
]
}
then run composer update
Related
I am using Laravel 5.8 and I created a custom class named StatusLib.php in the app/library folder.
StatusLib.php
namespace App\library;
class StatusLib
{
CONST SUCCESS = '100';
CONST SUCCESSWITHMESSAGE = '101';
}
I can call this status .
StatusLib::SUCCESS
When I add this following use code in the controller.
use app\library\StatusLib;
How can I add this StatusLib class in autoload and access from anywhere in the project?
Namespaces are case-sensitive.
In your StatusLib class you have App\library;, however, in your controller you've used app\library -- these are not the same.
Change your use statement in your controller to be:
use App\library\StatusLib;
You may also need to run:
composer dumpautoload
Just FYI, Laravel comes with the app directory already set up for autoloading.
In your composer.json file, after the classmap array, add a psr-0:
"autoload" :{
"classmap": [
...
],
"psr-0": {
"library": "app/"
}
}
Run composer dump-autoload.
Hope it helps.
where do you want to use it?
it will be automatically autoloaded because app folder is loaded in composer.json
here:
"autoload": {
"psr-4": {
"App\\": "app/"
},
},
I am using Laravel 5.3 and for testing purpose, I have created a folder called "Helpers" is "app" folder. In "Helpers" folder, there is a file named credentials.php where I will declare some strings then I will use it later. Credentials.php looks like
`
<?php
namespace App\Helpers;
class Credential
{
}
`
In the 'tests' folder there is folder called 'links' and in this folder there is file called something.php and it looks like
<?php
use App\Helpers\Credential;
class something extends TestCase
{
}
and its working correctly...Problem arising when I am trying to put the "helpers" folder from "app" to "tests" folder. I dont want to put the "Helper" folder in "app" directory. I have changed namespace and other things, nothing works. I am new to laravel and seeking your help and suggestion.
Update Composer.json and add files key in autoload section.
"autoload": {
"files": [
"tests/helpers.php"
],
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
After Adding this, run the following command.
composer dump-autoload
I use composer for autoloading classes but autoload didn't work. My structure of project looks like like this:
Commandos:
src:
here is located php classes
inside commandos folder composer json file.
{
"require": {},
"autoload": {
"psr-4": {"Commandos\\": "/src"}
}
}
In every class I use namespace like this namespace Commandos\Some; and require _autoload.php
Where is I was mistake?
I fix it my mistake.
I should use in composer.json
{
"require": {},
"autoload": {
"psr-4": {"commandos\\": "src"}
}
}
and in every my class in folder src use namespace begin from namespace commandos;
I have create in vendor directory the following structure for my package:
/vendor/
koala/
bamboo-sdk/
src/
Engine2/
Utils/
I then run
$composer init
inside koala folder.
Lately I went to Laravel5 composer.json main. And modified the autoload.psr-4 part like this:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
...
"Koala\\BambooSdk\\": "vendor/koala/bamboo-sdk/src"
}
},
I then run
$composer dump-autoload
in order to have /vendor/composer/autoload_classmap.php updated with the new references.
But I dont see it changing.
Also having a new controller with directive:
use Koala\BambooSdk\MyClass;
and then below
$myobj = new MyClass();
will result in a error:
FatalErrorException in MyController.php line 165:
Class 'Koala\BambooSdk\MyClass' not found
When running the example code on the laravel docs php artisan make:request StoreBlogPostRequest to create a new validation controller, I get the following error
[RuntimeException]
Unable to detect application namespace.
I'm not sure what's wrong, I've done some searching, but nothing really explains this error. Any ideas?
In Laravel 5, an "application" is a collection of PHP files under a single namespace, stored in the folder app/
By default, and in most of the Laravel 5 sample code from the docs, this namespace is App\. For example, one controller in your application might look like this.
namespace App\Http\Controller;
class MyController
{
//...
}
When Laravel generates code (i.e. when you use the make:request command), it needs to know what this application namespace is (it's possible to change the namespace with the artisan app:name command). For some reason, in your system, Laravel 5 can't detect the namespace.
If you look at the section of Laravel 5 core code that detects the namespace
#File: vendor/laravel/framework/src/Illuminate/Console/AppNamespaceDetectorTrait.php
protected function getAppNamespace()
{
$composer = json_decode(file_get_contents(base_path().'/composer.json'), true);
foreach ((array) data_get($composer, 'autoload.psr-4') as $namespace => $path)
{
foreach ((array) $path as $pathChoice)
{
if (realpath(app_path()) == realpath(base_path().'/'.$pathChoice)) return $namespace;
}
}
throw new RuntimeException("Unable to detect application namespace.");
}
You'll see that Laravel detects the namespace by looking at your composer.json file, and looking for thefirst valid psr-4 namespace.
My guess is your composer.json file is missing the namespace
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
Add that back in, and you'll be good to go.
Usually, this error can be mapped to syntax issues or errors in composer.json file. Check for any trailing commas or Auto load issue. For e.g.
"require-dev": {
"barryvdh/laravel-debugbar": "^3.5",
"phpunit/phpunit": "^7.5",
},
This should be..
"require-dev": {
"barryvdh/laravel-debugbar": "^3.5",
"phpunit/phpunit": "^7.5"
},
See no trailing commas at the end of "phpunit/phpunit": "^7.5"