php

Tired of the localhost setup like MAMP, WAMP or XAMP?
Tired of the configuration, ‘etc/hosts’, and stuff like that.

We’ve been experimenting with Laravel Valet for the last months now and decided not to go back.
Its super easy, fast and reliable.

Installation

Before you start, make sure no other service is using port 80.

We’ll get started with installing Homebrew, the latest version of PHP (7.4 at this moment) and Composer

– You can update your Homebrew with “brew update”
– Installing the latest version of PHP using Homebrew : “brew install php”

We’re going to install Valet globally to our machine so we’ll use the following command.

composer global require laravel/valet

Once installed we can run valet install

Defining the working directory

We need to attach Valet to an directory where we’ll keep our projects.
When navigated to the directory of your choice we can initiate this directory as the Valet directory by using the Park Command.

valet park

Once run, we can create new application projects in this directory.
The directory name of the project will also be the url for Valet. So for example is I have a directory called “pizzawebsite” I can use the URL http://pizzawebsite.test it’s as easy as that…

Sharing projects

It’s even possible to share a project with your team of client.
Thanks to the valet share command a publicly accessible url will be created using Ngrok.

Only Laravel projects? No, many more.

For those of thought Laravel Valet is only for Laravel projects… Nope !
It can be used for Laravel, Lumen, October CMS, Drupal, Craft CMS, Magento, WordPress and many more.

In this tutorial we’re only focussing on localization, so I won’t go over the installation of Laravel and I assume that you already have a working Laravel project.
We are going to implement localization in 5 simple steps.

Step 1. Provide a language route

In the routes/web.php file you can find all the routes for your project, so we will also put our language route in this file.
I prefer to also put my language function in the route, by doing this we don’t need to have a LanguageController for this simple task.

Route::get('lang/{locale}', function ($locale) {
    session()->put('locale', $locale);
    return redirect()->back();
});

In this function we’re setting the locale in our App and also providing a locale variable to our session that we’re going to use in the next step.

Step 2. Provide a Localization Middleware

In order to create this new Middleware we’ll run the following command in our terminal.

php artisan make:middleware Localization

Once this middleware is created by Artisan, we can find it in the app/Http/Middleware folder. To complete this middleware we’ll need to make some small changes to it, in order to get the behaviour we want. For starters we need to make sure it uses our App. this can be done by adding use App to it. We also need to make sure it sets the correct language. This can be done by setting the App locale value to what we have stored in our session.
The final result of this file should look like this.

namespace App\Http\Middleware;

use Closure;
use App;

class Localization
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    { 
        if (session()->has('locale')) {
            App::setLocale(session()->get('locale'));
        }
        return $next($request);
    }
}

Step 3. Register our Localization Middleware

In order to make the application use our newly created Localization Middleware we need to add it to the middelwaregroup. In the app/Http/Kernel.php file we need to add our Middleware class to the middlewareGroups.

The contents should then by like this.

    
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            \App\Http\Middleware\Localization::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

Step 4. Create language files

In the resources/lang folder there is a standard en folder, containing a few language files.
In this example we’re going to add our own language file to it called “customlang” and provide the following content to it.

return [

    /*
    |--------------------------------------------------------------------------
    | Custom Language Lines
    |--------------------------------------------------------------------------
    */

    'welcome' => 'Welcome',
    'chooselang' => 'Choose your language',

];

Now we can just make copy of the en folder and translate all content in it to a preferred language.
For example

return [

    /*
    |--------------------------------------------------------------------------
    | Custom Language Lines
    |--------------------------------------------------------------------------
    */

    'welcome' => 'Welkom',
    'chooselang' => 'Kies uw taal',

];

Step 5. Setup the view

Now we’re able to use these custom translations in our views.
For example:

<div class="content">
    <div class="title m-b-md">
        {{__('customlang.welcome')}}
    </div>
    <p>{{__('customlang.chooselang')}}</p>
    <div class="links">
        <a href="lang/en">EN</a>
        <a href="lang/nl">NL</a>
    </div>
</div>

Bonus

To add some extra flavour to it we can make use of a variable in our language file.
For example, we can add a username to our welcome message:

'welcome' => 'Welcome :user !',

To make use of this we need to update our view as follows:

<div class="content">
    <div class="title m-b-md">
        {{__('customlang.welcome', [ 'user' => 'Bert'])}}
    </div>
    <p>{{__('customlang.chooselang')}}</p>
    <div class="links">
        <a href="lang/en">EN</a>
        <a href="lang/nl">NL</a>
    </div>
</div>

Installing Laravel

Installing Laravel and getting your project going is quite easy, if you know how to do it. So here we go!

First of all you’ll need a local server.
You’ll be needing at least PHP version 7.1.3. and I’m currently working on PHP 7.2.

I prefer to install Laravel globally. Use the following command on your terminal.

composer global require "laravel/installer"

After Laravel is installed you can run the simple command.

laravel new myFirstProject

And it will create a new folder for you with the name myFirstProject with a fresh installation of Laravel in it.

The next step is to set up your vhost to your folder. (don’t forget to link it to the public folder)
Once this is done, you’ll be able to browse to your newly created project using the browser of your choosing.

Installing Laravel using these steps will automatically result in a secure installation, meaning that there will be a Application Key generated for you.

Setting up the database

Setting up a database for your project will be quite easy because we will be using the power of artisan to build our project.
The only thing you need to do manually is creating an empty database. Afterwards you’ll need to put in the credentials into the .env file, right over here:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

When this is done we can start using the database.

From this point forward you can start developing your project!