Laravel Style Guide

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.