PHP & Laravel

General

Code style must follow PSR-1, PSR-2 and PSR-12. For autoloading the PSR-4 standard is used. Generally speaking, everything string-like that’s not public-facing should use camelCase. Detailed examples on these are spread throughout the guide in their relevant sections.

Strings

When possible prefer string interpolation above sprintf and the . operator.

$foo = 'Hello, my name is ' . $name . '.';

$foo = 'Hello, my name is {$name}.';

Ternary operators

Every portion of a ternary expression should be on its own line unless it’s a really short expression.

$result = $object instanceof Model ?
    $object->name :
   'A default value';

$result = $object instanceof Model
    ? $object->name
    : 'A default value';

$name = $isFoo ? 'foo' : 'bar';

If statements

Curly brackets must always be present.

if ($condition) ...

if ($condition) {
    ...
}

Compound if statements

In general, separate if statements should be preferred over a compound condition. This makes debugging code easier.

if ($conditionA && $conditionB && $conditionC) {
    // Do something
}

if (! $conditionA) {
    return;
}

if (! $conditionB) {
    return;
}

// Do something

Avoiding else statements

In general, else should be avoided when possible because it makes code less readable. In most cases it can be refactored by using returns as quickly as possible.

if ($condition) {
    // Do something
} else {
    // Something else
}

if ($condition) {
    // Do something
    return;
}

// Do something else

Comments

Comments should be avoided as much as possible by writing expressive code. If you do need to use a comment, format it like this:

//There should be a space before a single line comment.

// If you need to explain a lot you can use a comment block. Notice the
// single * on the first line. Comment blocks don't need to be three
// lines long or three characters shorter than the previous line.

// There should be a space before a single line comment.

/*
 * If you need to explain a lot you can use a comment block. Notice the
 * single * on the first line. Comment blocks don't need to be three
 * lines long or three characters shorter than the previous line.
 */

Whitespace

Statements should have to breathe. In general always add blank lines between statements, unless they’re a sequence of single-line equivalent operations. This isn’t something enforceable, it’s a matter of what looks best in its context.

public function getPage($url)
{
    $page = $this->pages()->where('slug', $url)->first();
    if (! $page) {
        return null;
    }
    if ($page['private'] && ! Auth::check()) {
        return null;
    }
    return $page;
}

public function getPage($url)
{
    $page = $this->pages()->where('slug', $url)->first();

    if (! $page) {
        return null;
    }

    if ($page['private'] && ! Auth::check()) {
        return null;
    }

    return $page;
}

Doc Blocks

Doc Blocks are formatted according to the standard as used by Laravel. Note that the @param attribute is followed by two spaces, the argument type, two more spaces, and finally the variable name.

/**
 * Register a binding with the container.
 *
 * @param string|array $abstract
 * @param \Closure|string|null $concrete
 * @param bool $shared
 * @return void
 * @throws \Exception
 */
public function bind($abstract, $concrete = null, $shared = false)
{
    //
}

/**
 * Register a binding with the container.
 *
 * @param  string|array  $abstract
 * @param  \Closure|string|null  $concrete
 * @param  bool  $shared
 * @return void
 * @throws \Exception
 */
public function bind($abstract, $concrete = null, $shared = false)
{
    //
}

Laravel

A general rule is to stay as close as possible to Laravel’s own conventions regarding architecture and folder structure.

Views

Blade templates are essentially HTML templates with added functionality. Therefore the same HTML syntax guidelines are followed for Blade.

Blade files are located in the /resources/views directory, and use the .blade.php extension.

Naming conventions

Blade files should be named according to their related controller method.

Route Method Blade
/users index /users/index.blade.php
/users/create create /users/create.blade.php
/users/1 show /users/show.blade.php
/users/1/edit edit /users/edit.blade.php

Displaying data

Variables and functions should be written with a single space surrounding the variable or function name.

{{$name}}

{{route('home')}}

{{ $name }}

{{ route('home') }}

Loops

Loops should be written with a single space between the opening tag and the parentheses. Content inside the loop should be indented with a single tab, consisting of 2 spaces.

@foreach($items as $item)
{{ $item->name }}
@endforeach

@foreach ($items as $item)
  {{ $item->name }}
@endforeach

Conditionals

Conditionals follow the same code style as loops.

@if(auth()->user()->name)
Welcome back {{ auth()->user()->name }}
@endif

@if (auth()->user()->name)
  Welcome back {{ auth()->user()->name }}
@endif

Controllers

Models