I am trying to use ReflectionClass to get file name(from app directory) from a controller. For testing whether I can use the ReflectionClass or not i was trying in following way:
In my MyController.php
public function readContent()
{
$files = app_path() . DIRECTORY_SEPARATOR. "Drama.php";
// It returns "F:\xampp\htdocs\projectDirectory\app\Drama.php"
$class = new ReflectionClass($files);
echo "file name:: ". $class->getFileName();
}
I have a Drama.php file in this path. But when I am running the route for this method, I get following error
ReflectionException (-1)
Class F:\xampp\htdocs\projectDirectory\app\Drama.php does not exist
I have updated my composer.json file like following:
"autoload": {
"classmap": [
"app",
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
So that, i can read my app directory files.
I also ran the following commands
composer dump-autoload
composer update
php artisan config:clear
php artisan cache:clear
But i am still getting this error. Can anyone tell me how can i resolve this ?
Your issue here is ReflectionClass takes the class path as the constructor argument, not file path. try new ReflectionClass('\App\Drama') instead.
Related
I am using gabrielbull/ups-api in my laravel project
composer.json as follows
"autoload": {
"psr-4": {
"App\\": "app/",
"Ups\\": "vendor/gabrielbull/ups-api/src"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
The controller code as follows :
use Ups\Rate;
$rate = new Ups\Rate($accessKey, $userId, $password);
but I am getting an error
Class 'App\Http\Controllers\Ups\Rate' not found
Your controller can't find the Ups\Rate.
You should be able to do:
$rate = new Rate($accessKey, $userId, $password);
If not: You should be able to debug this quick with the following code.
require __DIR__ . '/vendor/autoload.php'
use Ups\Rate;
new Rate()
echo Rate::class; // output
It's a PHP package, so once you install it via composer, it's already autoloaded. You don't have to mess with the composer.json file. After installing run:
composer dumpautoload
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
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 have an RSA algorithm Library giving to me by a payment gateway and When I do a
include (app_path().'/PaymentGateway/Crypt/RSA.php');
this and try to make an object as $rsa = new Crypt_RSA(); this it gives me and error saying
Class 'App\Http\Controllers\Crypt_RSA' not found
I tried including it in web.php and making an object it worked the problem occur when I try to include it in a Controller.
This is what I did. Oh and a little back ground I use to have this in Laravel 4, PHP 5, jpgraph 2.
I am using jpgraph 4.1 on Laravel 5.5 using PHP 7.
Created a folder under app called jpgraph
Placed the src folder that is in the tarball of jpgraph in that folder
Created file call Graph1.php, is my code using jpgraph, with the class Custom_GraphsJM in the jpgraph folder.
In composer.json added "app/jpgraph/Graph1.php" to the "classmap"
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/jpgraph/Graph1.php"
],
"psr-4": {
"App\\": "app/"
}
},
In the application folder:
composer dump-autoload
Checked the autoload_classmap.php and I have
'Custom_GraphsJM' => $baseDir . '/app/jpgraph/Graph1.php',
In my Model at the top I have
use Custom_GraphsJM;
To create a class
$Two_Graphs_Temp = new Custom_GraphsJM();
You can tell Composer to autoload any (non-PSR) class by adding the base folder to:
"autoload": {
"classmap": [
"app/commands",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
....
And you can also autoload autoloaders by adding them to the files section:
"autoload": {
"files": [
"temboo/src/Temboo_Loader.php"
],
...
After adding those entries, execute:
composer dumpautoload
And check the file vendor/composer/autoload_classmap.php, the available classes must be all listed in it, if one file is not there it will not be autoloaded.
On default, everything included in the app folder of your laravel project is autoloaded, that is described in the composer.json of your project:
...
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
...
The only thing you will need to do is simply use the namespace:
use App/Path/To/Third/Party/plugin/Class;
If, however, the plugin is placed outside of the scope of App, then simply add it to the psr-4 autoloader:
"psr-4": {
"ProjectRootNs\\": "projects/myproject/"
}
Am trying structure my app in such a way that all my models will be in a dedicated directory(in my case Classified). I created the directory with Laravel app directory and added it to my my composer.json file. Below is the structure of my composer.json file:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"Classified\\": "app/",
"Classified\\": "app/Classified"
}
},
Then I run composer dump-autoload in my terminal but I keep getting "Key Classified\ is a duplicate in ./composer.json at line 29
" and when I tried viewing my app in the browser i get:
Fatal error: Uncaught exception 'ReflectionException' with message 'Class App\Http\Kernel does not exist' in /home/vagrant/Workspace/codulabproducts/classified/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 736.
Line 29 in my composer.json file is
"Classified\\": "app/Classified"
I don't know what am doing wrong because I have followed these steps in my other project and everything went well.
You can define more than one directory for a namespace prefix. But in that case the value for the key must be a list and not a string (see the second example in the documentation):
{
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"Classified\\": ["app/", "app/Classified"]
}
}
}
You can't have duplicated keys in your psr-4 mapping. It is supposed to define the root folder for given namespace and a namespace cannot have multiple roots.
Remove one of the mappings for Classified\ namespace.