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.
Related
I'm a bit confused because I'm programming a plugin for WordPress by using composer as it's the real way to go.
So I've created a composer file inside my plugin and some other stuff. In the composer file I've added my namespace for autoloading:
"autoload": {
"psr-4": {
"Johnny\\Lolkick\\": [
"includes/classes/"
]
}
}
Inside my classes folder I've created now a class with the name class-main.php. I've decided to take this name because of the WordPress naming conventions:
https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#naming-conventions
The class by itself was named class Main {. Inside my base plugin file I've created now a new instance of my class which failed. After changing the file name to Main.php it worked.
So in result the WordPress naming convention broke the autoloading of composer. I want to know now how do you handle this problem? How should I keep the naming convention by using composer?
Since your code base is not compatible with PSR-4 autoloading, a psr-4 mapping inside your composer.json's autoload section won't work, as you noticed.
I'd say you have two choices here:
First one would be to use classmap instead:
{
"autoload": {
"classmap": ["includes/classes/"]
}
}
This would simply parse all the files recursively within that folder and map the classes to their names, no matter what naming scheme you're following.
Second one would be to build your own autoloader, and use files to have it loaded automatically:
{
"autoload": {
"files": ["includes/autoloader.php"]
}
}
That autoloader would have to define what should happen (which class should be loaded, or not) when referring to a given class name.
In both cases, don't forget to run composer dump-autoload afterward.
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 made a class in php with some helper methods that parse HTML files.
I'd like to use this class in my Laravel project, but I'm new to Laravel and it's not clear how to add a simple class to a Laravel 5 project.
Is this possible? Or do I need to go to all the trouble of creating a composer package for my class, hosting it somewhere, and then require it in my composer.json file. That seems like a lot of work for including a simple PHP class, and I'm hoping there's an easier way.
As it stands right now there's not a great/easy way to do this in Laravel 5 (possibly by design). The two approaches you can take are
Create a new class in the App namespace
By default Laravel 5.0 looks for App\ prefixed classes in the app/ folder, so something like this should work
#File: app/Helpers/Myclass.php
<?php
namespace App\Helpers;
class Myclass
{
}
and then create your class with
$object = new App\Helpers\Myclass;
This approach, however, relies on you creating classes in the App\ namespace, and there's some ambiguity around if the App\ namespace is owned by Laravel, or is owned by the developer of the application.
Create your own Namespace and Register as PSR-4 autoloader
A better, but more complicated, approach would be to create classes in your own namespace, and then tell Laravel about this namespace by registering a new PSR autoloader.
First, you'd create the class definition
#File: application-lib/Myclass.php
<?php
namespace Pulsestorm;
class Myclass
{
}
Notice we've created a new folder off the root folder to hold our classes named application-lib. You could name this folder anything you like, because in the next step, you're going to add a section to your composer.json file's autoloader section
#File: composer.json
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Pulsestorm\\": "application-lib/"
}
},
The section we've added is this
"Pulsestorm\\": "application-lib/"
The key to the object (Pulsestorm\) is your namespace. The value (application-lib) is the folder where composer should look for class definition files with the specified namespace.
Once you've added this to composer.json, you'll need to tell Composer to regenerate it's autoload cache files with the dumpautoload command
$ composer dumpautoload
Generating autoload files
After doing the above, you should be able to instantiate your class with
$object = new Pulsestorm\Myclass;
The "real" right way to do this would be to create a generic composer package for your helper class, and then require that composer package into your laravel project. That may, however, be more work than you care to take on for a simple library helper.
If your class is generic enough to use it in other projects, the best way is to release it as a package.
Here's how you create packages with Laravel 5: http://laravel.com/docs/5.0/packages
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.
I have a project in laravel and I am trying to move all of my source files to app/src/{module}. I am autoloading the directory in composer.json.
I am having to declare/import all of the global classes from laravel in my files under src/. For example if I want to use Input I have to say use Input;. How can I access these classes as if the file was in the app/controllers directory?
What I added to composer.json:
"autoload": {
"psr-0": {"Illuminate\\Auth": ""}
},
The top of one of my files under src:
<?php namespace src\proposal;
use Input,JsonResponder,Request,JsonValidator,DB;
class ProposalRepo implements IProposal
{
Just because you are using your own namespaces, you will need to either use global classes like \Input::get() or you will need to put use Input; to the top of your file. At least this is how i solve this issue.