How and when to create packages in Laravel 4?

3

I have been researching and thinking about a subject for some days and I still do not understand how to do / use / start:

Scenario

  • Package: Books (crud)
  • Package: Authentication module (users + profiles + login + permission)

Doubt

I have 2 packages, but the packaeg Books belongs to (belongsTo) User: How do I do this?

  • How do I set up this relationship? (distinct packages)
  • How do I configure the dependency of a package? (User-dependent book to work correctly)
  • And if the authentication package does not exist, then the default files in the APP folder, how do I relate to the Books package?
  • Books and Authentication do they really need to be packages? That is: is this the real concept of packages? small modules, any library (upload for example), etc.)
  • It is more a conceptual question than a practical one, but I would like to know in practice how to effect relationships.

    If you need more information let me know.

    Thank you!

        
    asked by anonymous 24.09.2014 / 22:56

    1 answer

    1

    Good morning!

    If I understood correctly what your doubts were:

    Relationships, in laravel, are stored in models.

    Model Book, for example:

    public function user()
    {
        $this->belongsTo('User');
    }
    

    If you want the entire relationship to be called, you have to retrieve one or more books from the database, you can use the with attribute of the Eloquent \ Model:

    protected $with = ['user'];
    

    where 'user' is the name given in the above method.

    Laravel has a great permissions management, so you could define more rules in the User model, like:

    public function isAdmin () {     return ($ this-> isAdmin == true); }

    So you can check if the user is admin anywhere in the application, just by calling:

    Auth :: user () -> isAdmin ()

    Following the same courses you can make more complex and flexible validations using a table to store the rules or groups as well.

    Example of permission management used by Django in sqlite, without the constraints:

    CREATE TABLE 'auth_user' (
        'id'    integer NOT NULL,
        'password'  varchar(128) NOT NULL,
        'last_login'    datetime NOT NULL,
        'is_superuser'  bool NOT NULL,
        'group_id'  integer NOT NULL,
        'username'  varchar(30) NOT NULL UNIQUE,
        'first_name'    varchar(30) NOT NULL,
        'last_name' varchar(30) NOT NULL,
        'email' varchar(75) NOT NULL,
        'is_active' bool NOT NULL,
        'date_joined'   datetime NOT NULL,
        PRIMARY KEY(id)
    );
    
    CREATE TABLE 'auth_permission' (
        'id'    integer NOT NULL,
        'name'  varchar(50) NOT NULL,
        'content_type_id'   integer NOT NULL,
        'codename'  varchar(100) NOT NULL,
        PRIMARY KEY(id)
    );
    
    CREATE TABLE 'auth_group_permissions' (
        'id'    integer NOT NULL,
        'group_id'  integer NOT NULL,
        'permission_id' integer NOT NULL,
        PRIMARY KEY(id)
    );
    
    CREATE TABLE 'auth_group' (
        'id'    integer NOT NULL,
        'name'  varchar(80) NOT NULL UNIQUE,
        PRIMARY KEY(id)
    );
    

    This way you could perform a eager loading

    to determine whether a user has permission to access a particular location, along with the addition of filters that do this automatically.

    Packages are meant to make the project more organized, flexible and reusable, so you can use them in other applications without much change. Its use is not necessary, but recommended for large applications.

    Instead of building packages, you could create a folder inside / app called library and add it to composer.json:

    "autoload": {
            "classmap": [
                "app/commands",
                "app/controllers",
                "app/database/migrations",
                "app/database/seeds",
                "app/tests/TestCase.php"
            ],
            "psr-0": {
                "Biblioteca": "app/biblioteca"
            }
        },
    

    In each script inside this folder, you must use the namespace separation.

    A good example is the open source laravel-tricks .

        
    07.10.2014 / 16:00