How and when to create Packages in Laravel 4?

Asked

Viewed 180 times

3

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

Setting

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

Doubt

I have 2 Packages, but packaeg Books belongs to (belongsTo) User: how do I do this?

  1. How I set up this relationship? (distinct packages)
  2. How do I set up the dependency of a package? (user-dependent book to work properly)
  3. And if there is no authentication package but the default files in the APP folder, as I do with the Books package?
  4. Do Books and Authentication 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 doubt than practice, but I would like to know in practice also how to effect relationships.

If you need more information, let me know.

Thank you!

  • Well in my understanding, Books and Users are Entities of your application, not the packages. Your package would actually be just the Crud and the Authentication Module, where they consume the entity (or the Model).

1 answer

1

Good morning!

If I understood correctly what your doubts were:

The relationships, in Standard, are stored in the models.

Model Book, for example:

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

If you want the whole relationship to be called you have retrieved one or more books from the database, you can use the attribute with da model Eloquent Model:

protected $with = ['user'];

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

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

public Function isAdmin() { Return ($this->isAdmin == true); }

So you can check if the user is admin at any location of the application by just calling:

Auth::user()->isAdmin()

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

Example of permissions management used by Django in sqlite, without restrictions:

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)
);

That way you could accomplish one Eager loading

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

The packages aim 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 within /app called library and add 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 within this folder, namespace separation should be used.

A good example is open source Laravel-Tricks.

  • I appreciate your reply. But my question is more theoretical: when to create packages? based on what? And apart from that (a little technical), how to relate 2 packages? (dependencies, models, etc)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.