Laravel Style Guide

New Laravel Project Install:

  1. Download and install Composer- Laravel utlilizes Composer to manage various dependancies. Hence it is important to have composer installed on your machine.

  2. Install Laravel to Composer- Laravel can be called and executed locally on your machine, by running composer global require "laravel/installer". This command creates a new directory allowing for fresh Laravel installs to be specified.

  3. Create new Laravel project- Composer will create a new project with the latest Laravel version currently i.e. 5.7 composer create-project laravel/laravel test

  4. Check to see whether the new project generated successfully then the following changes can be made-

    • php artisan key:generate (encrypt data such as sessions, passwords and tokens),
    • php artisan serve (generate new Local Development Server),
    • Laravel development server started: <http://127.0.0.1:8000>

Existing Laravel Project Clone:

  1. Clone the project from GitHub- git clone project-directory-name

  2. Copy .env file on the root folder- If there hasn’t been an existing env file provided, run php artisan key:generate to generate a new .env file. Remember to change the DB_DATABASE, DB_USERNAME and DB_PASSWORD to correspond to your configuration.

  3. Database migrations- A database should have been provided, when the database has been provided or a new one has had to be made run php artisan migrate so that the latest changes to the database have been made.

  4. Check environment setup-

    • By running php artisan serve the environment can be configured to see whether the installation was successful. The resulting server can be run on this url, stated in the terminal Laravel development server started: <http://127.0.0.1:8000>.
    • If not it maybe worth running composer update in the project directory.

General PHP Rules

The formatting of our code must follow PSR-1 and PSR-2 where appropriate.

Configuration and view files

Configuration files and blade templates must use kebab-case.

config/
  contact-form.php

resources/
  views/
    thank-you.blade.php

Configuration keys must use snake_case.

// config/contact-form.php
return [
    'email_address' => env('PRIMARY_EMAIL_ADDRESS'),
];

Routing

All URLs should use kebab-case and should have hierarchy where necessary:

https://wecreatedigital.co.uk/contact-form
https://wecreatedigital.co.uk/contact-form/thank-you

Route names must use camelCase.

Route::get('contact-form', 'ContactController@index')->name('contactForm');
<a href="/contact-us">
    Contact Us
</a>

Controllers

Controllers with a resource should always be specified with a plural name.

class PostsController
{
    // ...
}

Where appropriate keep controllers simple and use CRUD keywords such as (index, create, store, show, edit, update, destroy). If you need other methods, create a new controller. For example in the below, we could have ProjectController@like, and ProjectController@unlike, or we could separate into another controller called FavouriteProjectsController.

class ProjectsController
{
    public function create()
    {
        // ...
    }

    // ...

    public function favourite(Post $post)
    {
        request()->user()->favourites()->attach($post);

        return response(null, 200);
    }

    public function unfavourite(Post $post)
    {
        request()->user()->favourites()->detach($post);

        return response(null, 200);
    }
}

Here we fall back to default CRUD words, create and destroy.

class FavouriteProjectsController
{
    public function create(Post $post)
    {
        request()->user()->favourites()->attach($post);

        return response(null, 200);
    }

    public function destroy(Post $post)
    {
        request()->user()->favourites()->detach($post);

        return response(null, 200);
    }
}

Blade Templates

Indent using four spaces.

<a href="/contact-us">
    Contact Us
</a>

Don't add spaces after control structures.

@if($valid)
    Yay
@endif

Comments

If writing clear and concise code as outlined in this guidelines then comments should only assist the reading process:

// Always leave space before a single line

/*
 * Integer posuere erat a ante venenatis dapibus posuere velit aliquet
 * cras justo odio, dapibus ac facilisis in, egestas eget quam
 * aenean lacinia bibendum nulla sed consectetur
 */

For every method should be a comment explaining its use.