Skip to content

Instantly share code, notes, and snippets.

@abdelaziz321
Forked from hofmannsven/README.md
Created February 25, 2018 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdelaziz321/becff44090348f8734ce2d88e9967905 to your computer and use it in GitHub Desktop.
Save abdelaziz321/becff44090348f8734ce2d88e9967905 to your computer and use it in GitHub Desktop.
Notes on working with Laravel 5

Working with Laravel 5

Tools

Autocompletion

Oh My Zsh

Within ~/.zshrc:

plugins=(git laravel5)

Usage

Show version: php artisan --version

Show all commands: php artisan list

Show all routes: php artisan route:list

Setup

Key: php artisan key:generate

Secure Dotfiles on Apache

<FilesMatch "^\.">
    Order allow,deny
    Deny from all
</FilesMatch>

Setup Homestead with Vagrant

Multiple PHP versions

Execute a command in the command line with a particular PHP version:

  • php5.6 artisan list
  • php7.0 artisan list
  • php7.1 artisan list

Fix authentication failure on bootup

Problem: $ vagrant up results in authentication failure.

default: Warning: Authentication failure. Retrying...

Solution: SSH into the VM via $ ssh vagrant@localhost -p 2222 and remove and regenerate the private keys:

wget https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub -O .ssh/authorized_keys
chmod 700 .ssh
chmod 600 .ssh/authorized_keys
chown -R vagrant:vagrant .ssh

Mcrypt

Install Mcrypt via Homebrew: brew install mcrypt

Multiple PHP versions

Install Mcrypt for a particular PHP Version: sudo apt install php5.6-mcrypt

Setup Permissions

Make sure to grant Apache access to store data within storage:

  • storage/app
  • storage/framework
  • storage/logs

See: https://gist.github.com/hofmannsven/8392477#permissions

Daily logs: Add APP_LOG=daily to your .env config file (see config/app.php).

Populate: php artisan db:seed

Populate a specific database: php artisan db:seed --database mysql_testing

Create new migration: php artisan make:migration create_tasks_table --create=tasks » see database/migrations

Migrate: php artisan migrate

Migrate, again: php artisan migrate:refresh

Migrate a specific database: php artisan migrate --database mysql_testing

Model

Create new model: php artisan make:model Task » see app

Create new model and do migration: php artisan make:model Task -m

Policy

Create new policy: php artisan make:policy TaskPolicy » see app/Policies

Create views: php artisan make:auth --views » see resources/views/auth

Create: php artisan make:middleware BeforeMiddleware » see app/Http/Middleware

Note: The name of the event is usally set in past tense (event is already done).

Add new event with listeners in app/Providers/EventServiceProvider.php

Create new events automatically based on your EventServiceProvider: php artisan event:generate » see app/Events and also app/Listeners

Create command: php artisan make:console AppAbout --command=app:about » see app/Console/Commands

Register new command: » see app/Console/Kernel.php

Usage: php artisan app:about

Usage: php artisan make:test UserTest

Configuration

Add testing database credentials to your phpunit.xml file:

<env name="DB_HOST" value="192.168.10.10"/>
<env name="DB_DATABASE" value="homestead"/>
<env name="DB_USERNAME" value="homestead"/>
<env name="DB_PASSWORD" value="secret"/>

Use temporary in memory database:

<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>

Composer

Re-generate autoload: composer dump-autoload

List dependencies of all dependencies: composer show --tree

Cache: php artisan route:cache

Clear: php artisan route:clear

Interacting with the application

Usage: php artisan tinker

E.g. create User: $user = factory(App\User::class)->create();

Performance & Caching

Caching for production

Composer: composer dump-autoload --optimize

Use controllers instead of closures within routes/web.php file to cache routes. Use route:cache to skip route compilation on each request:

php artisan route:cache

Use config:cache to reduce configuration parsing:

php artisan config:cache

The config cache includes config files and also the .env file.

Attention: Make sure to not use env() anywhere in the code base beside the config files! env() will return null with config:cache. Use config() helper function instead.

Generated cache files can be found in: /bootstrap/cache

Eager loading

Use eager loading to avoid the n+1 query problem. Save database queries using the with() helper function and reduce the amout of queries made in total.

User::with('relation_attr')->get();

Database chunking

Reduce server memory usage on queries returning lots of data to not overload the server. Attention: This may increase the database queries and overall loading time! Use chunk() helper function in the query builder.

MySQL indexing

Add index (KEY) columns based an where statemants whenever possible as MySQL is quite fast at indexing which will result in faster queries.

MySQL explain

Set the debug bar config to explain within /config/debugbar.php to explain each SELECT statement:

'explain' => [
  'enabled' => true
]

Multi-column indexes

Set multi-column indexes as new migration from database/migrations/add_table_name_indexes.php:

Schema::table('table_name', function(Blueprint $table) {
  $table->index(['user_id', 'created_at']);
});

Object caching

Use a key-value storage option like Redis or Memcached. Set Redis drivers within .env file:

CACHE_DRIVER=redis
SESSION_DRIVER=redis

And require predis via composer: composer require predis/predis

Make sure to keep an eye on Redis memory usage on user heavy applications and save as litte information as posssbile to Redis to reduce memory usage.

Cache usage

Use and combine auth()->user()->id and other dynamic parameters to generate a unique cache key with md5(). Use the decorator pattern when adding caching to the app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment