Laravel 4.x cheatsheet

Laravel documentation is good and is improving day by day but here is a brief and basic list of important points and methods that we need to work with.

Installation:

1 – Download source from github

2 – Put it into the webroot

3 – Install composer

4 – CD to that specific webroot directory and Run “composer install”

Configuration:

No configuration required. Although /public folder can be removed by

moving the files in the /public directory to root and renaming paths in the

index.php and paths.php in /bootstrap..htaccess has already the required

code to route requests to index.php.

Routing:

 

Input:

Views:

Controller:

Controllers extend from BaseController class and are named as

‘SomeController'(class as well as file)

Actions are public methods.

By default we cannot access it as http://www.domain.com/controller/action.

We have two options to do that:

1 – We route every action that needs routing

2 – We make our controllers restful to use HTTP vers like get/put etc.

For that we need to add controller as restful in our routes.php:

Route::controller(‘alias’,’SomeController’);

Then we can add methods such as ‘getUser’ in our controller and access it as

http://www.domain.com/controller/user

And if its multi worded then we will use ‘-‘s. Eg: getUserData will be

http://www.domain.com/controller/user-data

Restful controllers dont compel us to define route for every controller/action

Models:

We extend Models from the Eloquent class

protected $table; property has the actual table name

Conventionally we use plural table name and singular Model name like:

User model for users table

Saving data to model in controller:

Getting data:

Authentication:

Configuration is placed at config/auth

table and model can be changed there

Passwords encryption is managed by Hash::make($password);

For checking two hashed passwords :

We can access the logged in users credentials as :

It has password reminders and reset functionality out of the box

Events:

Facades:

Facaded are basically fronting classes that route methods as statics to their

actual classes. Every Facade extends from Facade class and has __callStatic

method that takes first method as the method name and the send as parameter and

find that function and parameters in the actual class. It makes the method calls

such as this :

as static, but they are not.

Forms:

Helpers:

Functions like app() app_path() etc.

IOC container:

This components makes an object available throughout the app:

It will be available as Someobject.

Mostly used in contributed packages.

Localization:

Localization files are plced in app/lang/{2letterlangcode}/filename.php

accessed like so:

Mail:

Mail::send() methods is simple and selfexplanatory arguments and we can use

sendmail for simple settings in app/config/mail.php

Package development:

We use Laravel command line tool Artisan to create a workbench package first:

> php artisan workbench vendor/package

This creates a package in workbench directory with complete structure. Note that it is just a workbench package and needs change in configuration to be used as an add-on.

All the code structure is placed at workbench/package/src/

It creates the directories for resources as well if you have used the –resources flag with the above command.

Pagination:

Security:

Hash::make() for encrypting password and Hash::check() for comparing passwords

Templating:

Blade templating is used:

{{{ or {{

Use {{{ for form echoing out user input

Templates are names as templatename.blade.php

And accessed as ‘folder.view’

Sections are defined as @section(‘sectionname’)

Sections can be overridden as:

Variables are echoed as {{{ $var; }}}

There are other language constructs equivalent to php constructs.

Main layout is defined in controller as :

Protected $controller = “view.name” ;

And sub views are attached to it using:

Testing:

Laravel supports phpunit out of the box and test classes can be placed at app/tests. Coverage reports and logs can be generated which can be used with SonarQube to analyse the code.

Validation:

=>Database:

Our model should extend from Eloquent

Artisan cli:

> php artisan *command* *options*

Some commands:

> Php artisan config:publish vendor/package

> Php artisan migrate:install

> Php artisan migrate:make foo –create = tablename

> Php artisan migrate:make foo_alter –table = tablename

> Php artisan migrate

Composer cli:

> composer install

> composer update

> composer dump-autoload

alexey

alexey

Application developer, movie buff, occasional reader and a huge Manchester United fan.
alexey

Application developer, movie buff, occasional reader and a huge Manchester United fan.