Where to create your own class, and how to instantiate it later?

16

I have a class of its own (i.e. external to the standard libraries of PHP and Laravel) that makes some connections with equipment of my network and in addition I need to create notifications routines by email and SMS.

For reference, this is the class :

<?php
/*****************************
 *
 * RouterOS PHP API class v1.5
 * Author: Denis Basta
 * Contributors:
 *    Nick Barnes
 *    Ben Menking (ben [at] infotechsc [dot] com)
 *    Jeremy Jefferson (http://jeremyj.com)
 *    Cristian Deluxe (djcristiandeluxe [at] gmail [dot] com)
 *
 * http://www.mikrotik.com
 * http://wiki.mikrotik.com/wiki/API_PHP_class
 *
 ******************************/

class routeros_api
{
    ...
}
?>

Question

Where to put the class files, and how to call them later from the application?

And if there is more than one viable option, what would be the best practices to keep third-party code organized?

    
asked by anonymous 15.01.2014 / 19:17

3 answers

9

I normally create a Library folder inside / app, and add it to the autoload of composer.json (autoload node -> classmap)

You can also add in the autoload of Laravel itself, in the file /app/start/global.php

Link: link

Note that by choosing the second option, you do not need to run the composer dump-autoload every time you create a class, or change the namespace, and so on, as opposed to the first option.

Or, you can create a package and put your classes in the vendor folder /

As you can see, Laravel is very flexible in this part, it is to your liking, there is no "cake recipe". What I can consider as "good practice" for your project may not fit, so it goes beyond your needs.

PS: When creating subfolders, be sure to use the namespaces correctly for autoload to work.

    
15.01.2014 / 20:12
4

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.

    
30.01.2014 / 05:11
2

With Laravel and Composer it's very easy to work with your own classes.

If you organize your classes well with namespaces, it's extremely easy to load them later

I do this:

  

app / Meupacote /

     

app / Meupacote / MyClasse.php

namespace Meupacote;
class MinhaClasse{}

and in composer.json you easily load them:

"autoload": {

    "psr-0": {
        "Meupacote": "app/"
    }
},

You can put all your application there, such as models and controllers

  

app / Minhaapp /

     

app / Minhaapp / Controllers /

     

app / Myapp / Controllers / HomeController.php

"autoload": {

        "psr-0": {
            "Minhaapp": "app/"
        }
    },

Remember doing so you should proceed to import the other classes since you are now working with namespace

namespace Minhaapp\Controllers;
use BaseController;
class HomeController extends BaseController{}

There will be no difficulty setting up your routes

Route::get('/', 'Minhaapp\Controllers\HomeController@index');
    
16.01.2014 / 13:19