For code directly related to the business rules of your application
I'm sorry to say that PSR-0 is being substituído
by PSR-4, which makes our life much easier to manage class directories, as it does not require you to create folders with the name of the namespace, which used to be confusing to most who tried to get started with PSR-0.
PSR-4 is available for use if your Composer is up to date:
[sudo] composer self-update
So I advise you to use a pattern similar to:
"autoload": {
"psr-4": {
"Empresa\Pacote\": "app/Pacote/"
}
},
Once you've done this, you just need to create the file Classe.php
in:
app/Pacote/Classe.php
And not like PSR-0, if you wrote the same, you would have to put in:
app/Pacote/Empresa/Pacote/Classe.php
And follow the normal namespace creation rules for your classes:
<?php
namespace Empresa\Pacote;
class Classe extends Whatever {
}
Then you just have to update the files autoloaders
of Composer:
composer dump-autoload -o
And probably he will have created the file
vendor/composer/autoload_psr4.php
With a line pointing to your folder, it will be vasculhada
if PHP needs to load any of the classes there.
For third-party code what you want to reuse
When it comes to third party code or your code that can be used in more than one application, ideally, in my opinion, it is to create a Composer package.
It seems complicated, but it's quite simple, follow the steps:
1) Create a repository for your package in github or bitbucket.
2) Create a folder anywhere, which does not yet have to be linked to your application.
3) In it create a composer.json file and in it you basically need to have:
{
"name": "empresa/pacote",
"description": "<descrição>",
"keywords": ["palavra-chave1", "palavra-chave1"],
"require": {
"php": ">=5.3.7",
},
"autoload": {
"psr-4": {
"Empresa\Pacote\": "src/"
}
},
"extra": {
"component": "package",
"frameworks": ["Laravel 4"],
"versions": {
"0.8.0": "master"
},
"branch-alias": {
"dev-master": "0.8.0-dev"
}
},
"minimum-stability": "dev"
}
3) Create the source files for the package inside the src/
folder. This folder name has become standard, but a lot of people use something different like lib/
.
4) Send your package to a git repository:
git init
git remote add <repositorio>
git commit -m "mensagem"
git push origin master
5) And finally, add the package to your project's composer.json:
"require": {
"empresa/pacote": "dev-master",
},
6) Since your package is not yet published in Packagist, you will need to tell Composer where his sources are, in case your VCS is Github, you will do:
"repositories": [
{
"type": "vcs",
"url": "https://github.com/seunome/pacote.git"
}
],
7) Just update the composer to see your new package being downloaded in your application:
composer update --prefer-source
I have added the prefer-source
option so that the composer downloads, in addition to the sources, the .git
directory so that you can edit your package directly in your application directory and commit the changes without having to leave of it.
I do not know if it sounded complicated, but I guarantee it's pretty easy.