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.
Related
I've created a package that creates a folder in the root of the project.
Creating it in the app folder is for me not clean enough. Cause I don't want it to look like it's merged with the laravel framework.
This package is for our company and will be used a lot.
So instead of changing the composer.json file everytime to add the folder to the autoloader I'm trying to just autoload it from the package.
Is something like that possible and how?
Are you saying that you do not want to add it in your composer.json file here?
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Company\\": "company/"
}
},
That is what I would do.
What if you just use the folder structure as the autoloader namespace? That should work. For example:
<?php
use Company\Foo;
new Bar();
Where you would have a folder called company/Foo with all the classes inside declaring their namespace like so:
<?php
namespace Company\Foo;
class Bar {
//
}
I'm working with Laravel framework.
I have a question regarding classes creation.
To explain my question, i will give you an example:
class articleController extends Controller{
public function bla(){
$a = new MyNewClass($colorfulVariable);
$b = $a->createSomething(new MySecondNewClass());
}
}
Now, the classes MyNewClass and MySecondNewClass do not exist, I want to create them, use them in every controller I want, just like I use the Redirect or Request classes that Laravel offers.
How can i create this classes? Make a new functions to use them in my laravel project?
you can just create those classes in your app folder (or in subfolder for example we create a Libraries folder with all the utilities). Just include a use App\YourClass; before using those classes and laravel will include the classes for you
Laravel 5.* is autoloaded using PSR-4, within the app/ directory. You can see this in the composer.json file.
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
Basically, you could create another directory within app, and then namespace your files in there as appropriate:
app/folder1/folder2/SomeClass.php.
Then, within your SomeClass.php, make sure you namespace it:
<?php namespace App\folder1\folder2;
class Someclass {}
Now, you can access this class using the namespace within your classes:
use App\folder1\folder2\SomeClass;
You can create your classes as usual, but don't forget to include them into composer.json autoload section:
"autoload": {
"classmap": [
"App\MyClassesNamespace"
....
And then run composer dumpauto command to rebuild autoloader file. If you'll not do this, you'll not be able to use your classes in your application.
You don't have to do anything other than below things:
Create a class in App\ folder.
Whenever you use the class, you will have to import the class using "use App\XYZ;" on top of the importing class.
thats it. Mostly people follow convention and then create "Service" folder on App\ folder. It is good to create alien class into service folder which are other than laravel framework classes.
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
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,
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.