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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | Route::get('/someroute','SomeController@action'); //For get requests Route::post('/someroute','SomeController@action'); //For post requests Route::any('/someroute','SomeController@action'); //To respond to any of above We can also use a closure function here: Route::any('/someroute',function(){ //Like showing a view straight away View::make('some.view'); }); Route :: to('home/test'); //For redirection within project space Route :: away('http://google.com'); //For external redirect |
Input:
1 2 3 | Input::get('varname'); //Getting request variables(post/get) Input::all(); // Getting everything |
Views:
1 2 3 | Views::make('some.view'); //Loading a basic view Views::make('some.view',array('data'=>$test)) //Passing data can be accessed as $data |
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:
1 2 3 4 5 6 7 8 9 | $model = new Model; $model->someColumn = $something; $model->someColumn1 = $something1; $model->someColumn2 = $something2; $model->save(); |
Getting data:
1 2 3 | DB::table('sometable')->where('some','>/</=','something')->first()/get(); DB::table('sometable')->get(); //Gets all results |
Authentication:
Configuration is placed at config/auth
table and model can be changed there
1 2 3 | Auth::check() // checking if authorized Auth::attempt(array('email'=>$email,'password'=>$password)) |
Passwords encryption is managed by Hash::make($password);
For checking two hashed passwords :
1 | Hash::check($password,$hashedPassword); |
We can access the logged in users credentials as :
1 | Auth::user()->{property} // eg Auth::user()->email |
It has password reminders and reset functionality out of the box
Events:
1 2 3 | Event::listen('event.name',function($data){ //do something }); Event::fire('event.name',$data); |
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 :
1 | View::make(); |
as static, but they are not.
Forms:
1 2 3 4 5 6 7 | Form::open() Form::label('labelname') Form::text('name') Form::close() |
Helpers:
Functions like app() app_path() etc.
IOC container:
This components makes an object available throughout the app:
1 2 3 4 5 | $this->app['Someobject'] = $this->app->share(function(){ return Someobject; }); |
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:
1 2 3 | App::setLocale('{2letterlangcode}'); Lang::get('{filename.php}.{arrayindice}'); |
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:
1 2 3 4 5 | DB::table(‘tablename’)->paginate($perpage); Then in views: $tablename->links; Will show pagination links |
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:
1 2 3 4 5 | @section(‘sectionname’) Newcontent @stop |
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:
1 | $this->layout->content = View::make(‘view.name’); |
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:
1 2 3 4 5 | $validator = Validator::make($rules,$input); $validator->failed() $validator->messages() |
=>Database:
Our model should extend from Eloquent
1 2 3 4 5 6 7 8 9 | Protected $table = ‘tablename’; Getting records: Modelname::find(); Modelname::all(); Modelname::where()->get(); |
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 install
> composer update
> composer dump-autoload
alexey
Latest posts by alexey (see all)
- Zend Soap NULL Reponse - August 29, 2018
- Quick Facts About WooCommerce - April 25, 2018
- WordPress stuck on a “Too many redirects” error loop when using SSL - January 17, 2018
Recent Comments