I can't change the namespace of my application in Laravel 5.8.
I'm using artisan to change it:
php artisan app:name TestApp
Result is: There are no commands defined in the "app" namespace.
Looking at php artisan you should have a php artisan app:name NewNamespace
command to change the namespace. Make sure that you are on the latest laravel version.
Old answer
To change the namespace of your app you have to edit the composer.json file:
"autoload": {
"psr-4": {
"CustomNamespace\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
but you also have to edit each file in the app subfolders and in the various configurations files (for example in config/app.php, config/auth.php, etc).
After you have done all of that you can run: composer dump-autoload
Keep in mind that this is an error prone method, because if you forget to replace a namespace in any of your files anything can stop working as expected.
Another option would be to create a custom package with the name you want and register it with a custom namespace. For example:
Create a lib/yourpackage/src folder in the root of laravel installation
Edit the composer.json file to load your custom code:
"autoload": {
"psr-4": {
"App\\": "app/",
"CustomNamespace\\": "lib/yourpackage/src/",
},
"classmap": [
"database/seeds",
"database/factories"
]
},
Run composer dump-autoload as before
You can now use anywhere your files in your project by referring to the custom namespace you have choosen.
laravel change this command to app:namespace
you have to do this
php artisan app:namespace TestApp
Are you trying to change the namespace of your application or just the name ?
What do you want to change exactly in the namespace ?
EDIT
I think you must have touched something in your application because the command php artisan app:name AppName should work, I just tested it.
Have you ever tried before to change the namespace by yourself?
Otherwise, try composer dump-autoload before to make sure your autoloading is up to date.
This feature has been removed as of Laravel 6. It's not recommended to change the namespace.
However if you want to do it, you can add the command to your Console/Commands directory
https://gist.github.com/isluewell/b824c0aef32f5007170fcd0d8498b657
Related
I have coded a package, and I have a problem with namespace I thinka
My plugin organisation is:
- database
-- factories
-- migrations
-- seeds
- resources
- src
-- MyServiceProvider
-- MyController
In composer.json, I have:
"autoload": {
"psr-4": {
"Xoco70\\LaravelTournaments\\": "src"
},
"classmap": [
"src/"
]
},
So basically, all database folder has no namespace.
When I want to call
php artisan db:seed --class=LaravelTournamentSeeder
I get:
[ReflectionException]
Class LaravelTournamentSeeder does not exist
But LaravelTournamentSeeder exists : first it exists in my plugin, then, it exists in my laravel installation, because I published the assets.
Any ideas???
It is a composer autoloading problem.
Just run command composer dumpautoload in your console from the project root, you should be able to run it.
I am struggling with PHP Namespaces and Auto loading in Laravel 5.4. I actually decided to put the models that I need to use in a definite and named folder as app/Models:
php artisan make:model Models/Customer
I have set Customer model's namespace to namespace Models;
Then, I created a route as follows:
Route::get('customer', function () {
$customer = \App\Models\Cutomer::find(1);
echo $customer;
});
To do auto loading work I opened the composer.json file located in the very root folder of the Laravel project and made the autoload to be as follows:
"autoload": {
"classmap": [
"database",
"app/Models"
],
"psr-4": {
"App\\": "app/",
"App\\Models\\": "App/Models"
}
},
After all of this I checked if my composer is an update version and ran dump-autoload:
composer self-update
composer dump-autoload -o
There is also worth of notice the contents of vendor/composer/autoload_classmap.php:
'App\\Models\\Customer' => $baseDir . '/app/Models/Customer.php',
'App\\Models\\Test\\Test' => $baseDir . '/app/Models/Test.php',
My problem is that whenever I execute the url: http://127.0.0.1:8000/customer I will encounter the following error output:
(1/1) FatalThrowableError
Class 'App\Models\Cutomer' not found
Would you please help me understand where of my work has been incorrect, and how to fix it? Thank you very much in advance.
The namespace in the model should be namespace App\Model; (hence why you call the class as \App\Models\Cutomer)
When you use php artisan make:model Models/Customer it should have set the namespace correctly then.
Also, you don't need to edit your composer.json to do this. Remove the additions you've made to your composer.json file for the autoloading and then run composer dumpautoload from the command line.
Hope this helps!
First, you have an error in your PSR-4 setup.
"App\\Models\\": "App/Models"
This does not map to a valid directory. Secondly, and more importantly, there is no need to autoload a nested namespace of one that is already declared. By autoloading App you will already autoload any nested namespaces as long as they conform to PSR-4 standards i.e namespace as directory name and filename as class name.
Composer setup only needs to be the following:
"psr-4": {
"App\\": "app/"
}
I wanted to start a new 3.3 project in Symfony and started as usual:
1.) Creating the new project: symfony new ArtProject
2.) Creating a new Bundle: php app/console generate:bundle (Paul/ArtBundle, yml, src/)
Then I run the local server and when I open 127.0.0.1:8000 I get this beautiful message:
(1/1) ClassNotFoundException
Attempted to load class "PaulArtBundle" from namespace
"Paul\ArtBundle". Did you forget a "use" statement for another
namespace? in AppKernel.php (line 19)
Which is strange and I haven't figured out why this happen so far. Before creating the Bundle, there was no error; I saw the typical startpage of symfony.
public function registerBundles()
{
$bundles = [
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
......
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new AppBundle\AppBundle(),
new Paul\ArtBundle\PaulArtBundle(),
];
}
<?php
namespace Paul\ArtBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class PaulArtBundle extends Bundle
{
}
Any idea whats going on there? I did not change a thing, I only ran these commands.
I just installed a fresh copy of S3.3.4 (latest version as of this writing) using:
composer create-project symfony/framework-standard-edition s334 "3.3.4"
bin/console generate:bundle
Share across multiple apps: yes
namespace: Paul\ArtBundle
bundle name: PaulArtBundle
Target Directory: src/
Refreshed the browser and sure enough I got the class not found message.
The generate:bundle command is not updating the autload section of composer.json when a new namespace is introduced. Edit composer.json and:
# composer.json
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle",
"Paul\\": "src/Paul"
},
"classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
},
Then run
composer dumpautoload
And restart the server(maybe). That should fix the bundle class not found problem.
I have not used the generate:bundle command is quite some time ever since the standard was to put everything under AppBundle so I don't know how long this has been "broken". But at least three questions in the last week or so indicates it was something recent.
And by the way, when I refreshed the browser I got "Hello World" which threw me for a bit. Turns out the new bundle overrides the / route which is also sort of special.
And in case anybody is wondering why this started happening, Symfony 3.2 changed from
#composer.json
"psr-4": { "": "src/" },
To
"psr-4": { "AppBundle\\": "src/AppBundle" },
You could always just change it back though I think spelling out individual namespaces might be "better". Not sure.
And here is an issue with more details: https://github.com/symfony/symfony-standard/issues/1098
Looks like the maintainer favored a tiny speed improvement over breaking an existing command. Oh well. Symfony Flex is supposed to make everything great again.
If you generate a bundle for usage in multiple projects (with own namespace) you need to add it in the composer.json as follwed:
Lets assume your bundle name is CompanyFooBundle with namespace Company\Bundle\FooBundle then the composer autoload section should look like:
...
"autoload": {
"psr-4": {
"Company\\Bundle\\FooBundle\\": "src/Company/Bundle/FooBundle"
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
...
This works for me in:
Generate your Bundle with
./console generate:bundle
And follow the steps as always, now, do what you want in your composer.json file with the line
"AppBundle\\": "src/AppBundle"
Replace with "": "src/" or add your bundle, for example: "BackendBundle\\": "src/BackendBundle"
Here's the new part:
Install composer in your bin directory, copy and paste the steps from https://getcomposer.org/download/
Up a level in your project directory, and in your root folder (of your project) run the next command
php ./bin/composer.phar update
Removing vendor dir and again running composer install helped me with same problem.
Im relative new in laravel, before use laravel I used a file functions.php with all functions that I use in my projects (create slugs, format dates, etc).
where I can create my own functions in laravel to use in controllers and views like myfunction($data) ?
Use the Laravel composer example, where it creates some helper files:
"autoload": {
"files": [
"src/Illuminate/Foundation/helpers.php",
"src/Illuminate/Support/helpers.php"
],
"psr-4": {
"Illuminate\\": "src/Illuminate/"
}
},
Edit your composer.json file, where you could create a helper in the App folder
"autoload": {
"files": [
"app/helpers.php",
],
...
},
Then you just have to execute
composer dumpautoload
To tell composer to load it automatically.
In your app/Http directory, create a helpers.php file to add your functions
Within composer.json, in the autoload block, add:
"files": ["app/Http/helpers.php"].
On command line run "composer dump-autoload"
While Working on a project using Laravel 4 to be precise, I decided that i wanted to make my own helper file to house my custom functions.. one of which is this below...
function pr($ar=array(), $bool=false){
echo '<pre>';
print_r($ar);
echo '</pre>';
if($bool){
exit;
}
}
in my composer.json file, just after the autoload: classmap , i added myne, autoload:files -arrar and included my custom file, app/helpers as illustrated below..
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"others":[
"app/helpers.php"
]
and i switched to my terminal window and ran the following commands
composer dump-autoload -o
but i still got errors that my pr() function was undefined... then i tried the artisan alternative... [-o ] to optimize the files
php artisan dump-autoload
but still it refused to work... and then i changed the array name from
"others":[
"app/helpers.php"
]
to
"files":[
"app/helpers.php"
]
then i got the desired response, my code could now see the custom function i wrote, please i'd like to know if there is a pattern i was supposed to follow or otherwise, in my case, i mistook " files ", for " others " and i got errors, but incase, what did i miss here, all i see is just a name-string value for the array representation....
This is how composer works. In autoload section you need to use files when you want to load some files. In my Laravel 5 project I have for example:
"autoload": {
"classmap": [
"database",
"tests/TestCase.php"
],
"psr-4": {
"App\\": "app/",
"verify\\": "verify/"
},
"files": [
"app/Helpers/functions.php"
]
},
If you look at documentation you will see that you need to use files to load any extra files by autoloader.
According to the official documentation
Currently PSR-0 autoloading, PSR-4 autoloading, classmap generation
and files includes are supported. PSR-4 is the recommended way though
since it offers greater ease of use (no need to regenerate the
autoloader when you add classes).
So the reason that "others" did not work was because it is not supported by composer. "others" is simply meaningless, while "files" actually have a specific autoloading mechanism.