Class '\User' not found - php

I am attempting to move all my User code in to it's own package for potential use on multiple projects, everything is working fine except for when I try to login and I get the following error:
Class '\User' not found
Now, I am assuming that this is because for some reason it is not finding the User model (I am using the default one that comes with Laravel but just moved to
'Vendor/VendorName/PackageName/src/models'
I can see that I can reference the User model from app/config/auth.php but if I change this to my package namespace I am seeing the same error but with the different path, also, I don't think that this is the best way to do this as for every project I will need to set the app/config/auth.php model which could prove to be pain, especially as I forget things!

My guess is that you have not autoloaded User. Below is an example of how Laravel do use autoload in composer, the User class is at installation placed inside "app/models", which is autoloaded there. If you have moved your User anywhere else you need to autoload it from there.
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models", // original path for User.php
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"your/new/path" // here you add your new path that should autoload
]
}
And do not forget to run composer dump-autoload after updating your composer.json file!

Related

Where is the right place to put files in Laravel?

I am new to Laravel and am trying to find my way around.
I want to create multiple custom Exception classes. I am a little confused as to where they should reside.
Should I create a folder in 'app' and place them in there and include the files manually from global.php?
Should I create a service provider?
For now, I have created a folder called 'exceptions' in app and added the path to ClassLoader::addDirectories. I could really do with some advice.
Are you using Composer? For example, using Composer you can put your configuration for autoloading:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/database/migrations",
"app/database/seeds"
],
"psr-4": {
"Exceptions\\": "src/Exceptions",
"Services\\": "src/Services",
"Api\\": "src/Api"
}
},
As result your Exception file /src/Exceptions/Specific/ExtraSpecificException.php will be available
namespace Exceptions\Specific;
class ExtraSpecificException extends \Exception
{}
Laravel doesn't have a set place to store custom exceptions, you can place them anywhere you like.
Creating an app/exceptions/ directory works fine - You can autoload them all in your global.php file by adding
app_path() . '/exceptions/'
to the ClassLoader::addDirectories array.
If you have a lot and prefer to be more organised you can namespace your exception classes and take advantage of PSR-4 autoloading with composer.

Laravel : Introducing custom Classes/Libraries

I have just started learning Laravel and during the process, I found out that we can introduce our custom classes into Laravel using the following:
Create a folder say app/MyLib
Create my class inside app/MyLib, say I created MyDates
Now modify the ClassLoader::addDirectories inside the app/start/global.php as follows:
ClassLoader::addDirectories(array(
...
app_path().'/MyLib'
));
Access MyDates class, however I want
I then came across this article Laravel 4 Application Setup: App library, Autoloading, Binding that uses composer to autoload the custom libraries. Now the question is, what's the best way to introduce my custom libraries in Laravel i.e. what's the recommended approad and if there are any differences between these approaches, what are those?
its best practice and the only way you should do it, if you modify your composer.json as follows
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
/* HERE YOUR LIBRARY FOLDER */
"app/MyLib",
]
},
EDIT:
You should run after the change composer dump-autoload to autoload your changes

laravel 4 view, where to put class based composer

I found that I can create a callback function using view composer
http://laravel.com/docs/responses#view-composers
so how to use class based composer:
View::composer('profile', 'ProfileComposer');
where to place ProfileComposer class?
thanks,
The view composer class should be defined as any regular class and might be stored in a libraries folder or if it is only used by a model you might store it there, there is no convention of where to store it. The class can hold some processes you want to reuse and you can register the call in a serviceprovider. This is a great tutorial on how to use it.
http://culttt.com/2014/02/10/using-view-composers-laravel-4/
You can have that ProfileComposer class inside ProfileComposer.php file and that will be in anywhere you want, if that file is autoloaded. You should be watching a video tutorial about Composers in Laracasts, and it will explain you why we have to use View::composer and how we can use.
I just find the answer in official decumentation.
you can put composer files anywhere in application file system, ex app/composer
and add it to composer.json
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/filters",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/composer"
]
},
then run artisan autoload:
php artisan dump-autoload
thanks everybody,

How to integrate a 3rd party class library with Laravel so it autoloads?

I am trying to integrate the Temboo SDK with the Laravel framework so that it autoloads like the rest of the vendors.
The SDK has the following structure:
temboo
src
library
temboo._23andme.php
temboo._37signals.php
etc...
temboo.php
Within the main Temboo file, they have multiple class declarations and each one uses naming such as class Temboo_Session and the classes in the library dir are of the form class _23andMe_Names extends Temboo_Choreography.
The temboo.php class file also includes an autoloader class Temboo_Loader and declaration spl_autoload_register(array('Temboo_Loader', 'autoload'));
This is my first time trying to integrate a non-PSR-0 library, so I am a little lost on this.
Any help would be appreciated.
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.

Download tcpdf manually without using composer in Laravel 4

I'm trying to download TCPDF library to my project using Composer (Laravel 4), but I can't.
Sometimes this error occurs
(http://i.stack.imgur.com/aaPDz.jpg)
and sometime this error
(http://i.stack.imgur.com/quXMB.jpg)
I want to download it and add it in laravel manually without using composer.
When you say "without using composer", I'm going to assume you mean "without using composer for the download". With the following solution you'll still need to invoke a composer command, but its just to get the library auto-loading.
First step is to find a folder that makes sense for storing your local copy of TCPDF. I would recommend against using the vendor folder, because that folder is largely (solely?) managed by composer. For the sake of demonstration, let's create a new folder called app/vendor. Not the best choice, I know, but this is just a demonstration of one possible solution. Download TCPDF, unzip it and move the resulting tcpdf folder into app/vendor (so you should end up with app/vendor/tcpdf).
Second step is to add this folder to the autoload section of composer.json, as follows:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/vendor/tcpdf" // <== HERE IT IS
]
Finally, run composer dump-autoload.
You should now be able to use the TCPDF library within your code without any external download dependencies. I tested this solution on a clean copy of Laravel 4.1 and it worked fine.
If anybody has a more appropriate suggestion as to the location of the tcpdf folder, please add a comment.

Categories