Younes is a freelance web developer from Morocco, passionate about back-end development, Laravel addict. In his free time, he enjoys playing soccer. You can learn more about him on his website.

Younes's articles

  1. How to Create a Laravel CSS-Minify Command

    In this article you’ll learn how to use Laravel’s Artisan command line tool, and how to create a customized command. Note that you need to be familiar with the Laravel framework to get the most of this article.

    What are we building

    In this tutorial we’re going to build a command to minify our css assets, which will be used like this:

    cssmin 'output_path' 'file1'...'fileN' --comments --concat
    • output_path: (required) path to save the minified files, (style.css -> style.min.css).
    • file1 ... fileN: (required) list of files to minify.
    • --comments: (optional) add this option to keep comments.
    • --concat: (optional) concatenate the minified files into one file called all.min.css.

    What is a Laravel Command

    Artisan is the name of the command line utility in Laravel. It comes with a set of predefined commands, which you can list with php artisan list. If you want to show the help for a specific command, you can use php artisan help command.

    Creating the Css Minifier Command

    To create an artisan command, you can use the command:make command. This command accepts one argument:

    • name: the class name for the command.

    and three options:

    • --command: the name that should be typed to run the command.
    • --path: by default the commands are stored within the app/commands folder, however, you can change that with this option.
    • --namespace: you can use this option to namespace your set of commands, e.g. in the command command:make, the make command is under the command namespace.

    Now, to create our command we will use php artisan command:make CssMinCommand --command=cssminwhich will create a CssMinCommand.php file within our app/commands directory.

    use Illuminate\Console\Command;
    use Symfony\Component\Console\Input\InputOption;
    use Symfony\Component\Console\Input\InputArgument;
    
    class CssminCommand extends Command{
        protected $name = 'cssmin';
        protected $description = 'Command description.';
    
        public function __construct(){
            parent::__construct();
        }
    
        public function fire(){
            //
        }
    
        protected function getArguments(){
            return array(
                array('example', InputArgument::REQUIRED, 'An example argument.'),
            );
        }
    
        protected function getOptions(){
            return array(
                array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
            );
        }
    }

    Our CssMinCommand class extends the Illuminate\Console\Command and overrides two methods ( getArguments, getOptions ).

    • getArguments: this function returns an array of arguments that should be passed to the command, (ex: the list of files that we pass to the cssmin command).
    • getOptions: returns a list of options or switches that you may pass to the command. (e.g. --comments).
  2. Dependency Injection with Laravel’s IoC

    As developers, we are always trying to find new ways to write well designed and clean code by adopting new styles, using design patterns, and trying new robust frameworks. In this article we will explore the dependency injection design pattern through Laravel’s IoC component and see how it can improve our design.

    Dependency Injection

    Dependency injection is a term coined By Martin Fowler, and it’s the act of injecting components into your application. Like Ward Cunningham said:

    Dependency Injection is a key element of agile architecture.

    let’s see an example:

    class UserProvider{
        protected $connection;
    
        public function __construct(){
            $this->connection = new Connection;
        }
    
        public function retrieveByCredentials( array $credentials ){
            $user = $this->connection
                            ->where( 'email', $credentials['email'])
                            ->where( 'password', $credentials['password'])
                            ->first();
    
            return $user;
        }
    }

    If you want to test or maintain this class, you would have to access a real database and do some queries. To avoid having to do that and to decouple the class from the rest, you have one of three options to inject the Connection class without actually using it directly.

  3. Memberships with Laravel Cashier

    Laravel is one of the most promising frameworks, and best described as :

    Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable, creative experience to be truly fulfilling. Laravel attempts to take the pain out of development by easing common tasks used in the majority of web projects, such as authentication, routing, sessions, and caching.

    In this tutorial we’re going to see how easy it is to create a basic paid membership website using the Laravel Cashier package.

    Setting up Working Environment

    We need to create a boilreplate project to get started, and we can do that in two different ways:

    • we can clone the Github repo to our project folder.
    • Assuming that you have composer installed, ( check installation guide for more details).
      we run composer create-project laravel/laravel laravel_membership --prefer-dist, this will create a laravel boilerplate project in our laravel_membership folder.

    Now we need to require the Laravel Cashier package to the project by adding "laravel/cashier": "~1.0" to our composer.json on the require section and run composer update to update our dependencies.

    After that we need to tell our app to load the Cashier service provider class. We can do that by adding "Laravel\Cashier\CashierServiceProvider" to the providers array on the config/app.php file.

    Note: You must run composer dump-autoload to update the classMap package.

    Creating the Database Using Migrations

    If you are new to Laravel Migrations be sure to check the documentation.
    We are going to use two tables:

    posts table:
    INT id
    STRING title
    LONG_TEXT content
    BOOL is_premium

    users table:
    - INT id
    - VARCHAR(60) email
    - VARCHAR(60) password

    The Laravel artisan command line tool makes it easy to create and manage migration classes.

    php artisan migrate:make create_posts_table --create="posts"
    
    php artisan migrate:make create_users_table --create="users"

    and then we fill the Schema::create callback function argument with the necessary code that looks like this