The Missing Laravel Commands have arrived.

We all had problems with creating files manually such as repositories, observers, facades etc..

Well, It took me a bit until I was really frustrated about such a routine process , then I came into an appropriate solution that relieved me from that, I created my own package that takes care of these stuffs.

Today I would like to introduce you Artify which is basically a package that helps you to create files for each common design pattern we use ( If there is something isn’t prevailed at the moment, feel free to mention and I’ll add it later )

Let’s take a fast look about what it does.

Artify Helps you to do the following :

  • Observer Generator.
  • Responsable Interface Generator
  • Synchronizing Policy & Gates with Roles table.
  • Facade Generator
  • CRUD Generator
  • Repository Generator
  • Authorization methods.

as a fresh start for artify , That’s not bad so far. 
Now, Let’s dig into these commands.


Install the package

That’s pretty easy, just require the package by the following command

composer require sectheater/artify:dev-master

If your laravel version is below 5.5, You have to register the service provider, otherwise that, The package is already discovered.

// in your config/app.php under the providers key
\Artify\Artify\ArtifyServiceProvider::class,

Now, You got to run this command in order to have Artify in action.

php artisan artify:install -c 20

Command Interpretation

1- It publishes the roles migrations,its seed, and the config file that artify will use.

2- “-c” stands for the caching whether to enable or not, 20 stands for the number of minutes that’s going to be cached.

If you don’t want to enable the cache just remove the -c and the number to become like that

php artisan artify:install

3- It asks about the Role model, and the permissions column which will artify handle during the generating files related to roles.

Alright enough talking about installation, Let’s enjoy being a professional artisans.


Observer Generator

Observer is a great design pattern that observes a particular eloquent event while interacting with the database layer such as updating,deleting,inserting etc..

Perhaps you need to fire one of these events to save some time and avoid coding a specific process several times , for example inserting user_id, or slugyifing the titles, or whatever your purpose is.

You can run the following command to have an observer

php artisan artify:observer PostObserver -m

That’s going to generate you an observer with the associated model to this Observer which is Post model in our case, There you can register your eloquent events, and Artify has registered this observer within your Service Provider automatically, so you are set to go now.

If you don’t know what are observers, I recommend you watch this series


Responsable Interface Generator

This Interface has been added since laravel 5.5, which allows you to strip out the logic from your controller, and put it there to make your code way cleaner.

php artisan artify:response PostShowResponse

This command only accepts one argument which is the name of the response, and it’s going to create you the responsable interface under App/Responses

If you don’t know how this interface works,I recommend you this tutorial


Synchronozing Your Roles table with Policy & Gates

This command is in charge of converting the available roles within your database into policy and gates

It’s preferable to follow the convention of naming the roles as mentioned in the Roles Table Seeder

Artify also supplies you with the migration for roles, if you are going to change the permissions column, don’t forget to update that within the config file

Permissions column should be an array, if they are jsonb in your database, use the accessors, or observers, or use casts property to convert permissions to an array

// Role Model
protected $casts = ["permissions" => "array"];

By Default this command requires the hasRole method ( it should return boolean ) , you can create your custom one within user model, or just import the Roles trait

use Artify\Artify\Traits\Roles\Roles;
 Class User Extends Model {
use Roles;
}

This method is required to check whether the user has a specific role to access somewhere or not, It’s used within the gates. Feel free to change the gates”s logic to suit your needs.

php artisan artify:register-authorization

By Firing this command, you can use the can middleware anywhere within your route file, and that’s it for the policy and gates, everything is created behind the scenes and you are set to use them.

Route::get("/post/create","PostController@create")->middleware("can:create-post");

also you can access the method can anywhere in your project.

if($user->can("create-post"))
// do something

If you want to discover more about what policy and gates can do, You should see the docs


Assign User To A Specific Rank.

This command is just there to save you a couple of minutes whenever you assign a user manually to a specific role through database.

php artisan artify:assign <username> <slug>
// Example :
php artisan artify:assign Alex admin

Create A Repository

Repository pattern is absolutely a powerful pattern that you should use in order to separate logic from your database layer ( model ) and keep your model cleaner.

php artisan artify:repository <name of repo> (-m?) (-f?)
  • -m stands for the model associated with the repository.
  • -f creates a facade for this repository , so you can call it immediately . i.e you can just do that anywhere \MyRepository::method()

If you don’t know about repositories, you can see this here.


Generate a facade.

php artisan artify:facade <name>

That registers your facade, and your alias within the config/app.php file.

If you don’t know about facades, You can refer to the docs


Generate CRUD

This one was actually inspired by Devlob’s article

Well, This artifier is really a beast, it could save you up to 20 minutes or something , It helps you to generate the following

  • Model
  • Resource Controller with the common logic you mostly do within your methods.
  • Request Form files for your store and update methods.
  • Resource Route within your web file
  • If Repository Option is enabled, It will create you the repository associating with the model.
php artisan artify:crud <model-name> (-r?)
// -r is optional , it will generate you the repository.
Example :
php artisan artify:crud Post -r

This is going to generate you the upon things, and since Repository option is enabled, The controller’s logic is going to use repository associated with the model which in charge of this controller , instead of the model directly.


The last thing to cover is the Roles Trait that artify supplies you with.

you can view what this trait does in detail here.


Still here? , If you like the package ,don’t forget to share it widely and please feel free to contribute with your suggestion and the other commands that should be added to this package to make it better.

Don’t forget to star the package also.

Alright, that’s it for this one, I see you at another article, Don’t forget to follow me to stay in touch with my latest articles.