Laravel Guide


Our Laravel Framework guide. In our best practices we aim to put together all the resources using the Laravel Framework.

  1. New Laravel Project Install
  2. Existing Laravel Project Clone
  3. General PHP Rules
  4. Configuration and view files
  5. Routing
  6. Controllers
  7. Blade Templates
  8. Comments
  9. Community Resources

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. If not it maybe worth running composer update in the project directory. Laravel development server started: <http://127.0.0.1:8000>

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 have a clear hierarchy:

https://wecreatedigital.co.uk/example
https://wecreatedigital.co.uk/example/example2
https://wecreatedigital.co.uk/example/example2/3
Route::get('example', 'ExampleController@example')->name('example'); // first-level
Route::get('example/example2', 'ExampleController@exampleTwo')->name('example2'); // second-level hierarchy
Route::get('example/example2/{id}', 'ExampleController@exampleThree')->name('example3'); // third-level hierarchy requiring a parameter
<a href="{{ route('example') }}">
    Example 1, first-level hierarchy
</a>

<a href="{{ route('example2') }}">
    Example 2, second-level hierarchy
</a>

<a href="{{ route('example2', ['id' => $id]) }}">
    Example 3, third-level hierarchy, must have parameter
</a>

Controllers

Where appropriate keep controllers simple and use the CRUD methods such as (index, create, store, show, edit, update, destroy). These methods can be automatically generated in the terminal by creating a new Resource Controller php artisan make:controller ExampleController --resource.

If you need other methods that follow under the same category, it is best to create a new separate Controller or if your Controller only has one action then use the __invoke() method as shown below.

class CRUDExampleController
{
    /**
    * Show the form for creating a new resource.
    *
    * @return \Illuminate\Http\Response
    */
    public function create()
    {
      return view('view-create-new-model');
    }

    /**
    * Store a newly created resource in storage.
    *
    * @param  \Illuminate\Http\Requests\StoreRecordRequest  $request
    * @return \Illuminate\Http\Response
    */
    public function store(StoreRecordRequest $request)
    {
        Record::create($request->validated());

        return response(null, 200);
    }
}

class SingleActionExampleController
{
    /**
    * @param  \App\Model  $model
    * @return View
    */
    public function __invoke(Model $model)
    {
      return view('view', ['model' => $model]);
    }
}

Blade Templates

Indent using four spaces.

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

Comments

When the code written is not entirely clear, it may be best to refactor into more readable code. If this is not the case then please use comments to assist the reading process. For example:


/*
 * 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
 */
public function someMethod(Collection $items)
{
    // Comment
    $process_this = [$items];

    // Another Comment
    $then_that($process_this);

    return response(200);
}

Community Resources

What we currently use

Other