getting to know laravel 5

29
LARAVEL 5

Upload: bukhori-aqid

Post on 11-Jan-2017

1.599 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Getting to know Laravel 5

LARAVEL 5

Page 2: Getting to know Laravel 5

What is Laravel?

• PHP Framework

• Most popular on Github

• MVC Architecture

• MIT License

Page 3: Getting to know Laravel 5

Why Laravel?

• Composer

• Community

• Code Construction

Page 4: Getting to know Laravel 5

@bukhorimuhammad

Less Talking, More Coding

Page 5: Getting to know Laravel 5

Requirements

• Composer

• Local server (with XAMPP, WAMP, MAMP, etc)

• PHP 5.4+

• Mcrypt, OpenSSL, Mbstring, Tokenizer & PHP JSON extension

Page 6: Getting to know Laravel 5

COMPOSERComposer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.

https://getcomposer.org

UNIX : curl -sS https://getcomposer.org/installer | php

WIN : https://getcomposer.org/Composer-Setup.exe

Page 7: Getting to know Laravel 5

Installing Laravel

Composer Global :composer create-project laravel/laravel —-prefer-dist foldername

Composer Local :php /path/to/composer.phar create-project laravel/laravel --prefer-dist foldername

Page 8: Getting to know Laravel 5

Fix Folder Permission

• Set storage folder to be writable (777)

• Set vendor folder to be writable (777)

Page 9: Getting to know Laravel 5
Page 10: Getting to know Laravel 5

@bukhorimuhammad

Laravel Concepts

Page 11: Getting to know Laravel 5

Laravel ArtisanArtisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for your use while developing your application. It is driven by the powerful Symfony Console component.

php artisan list : list all available artisan command

php artisan help [command] : help information for each command

http://laravel.com/docs/5.0/artisan

Page 12: Getting to know Laravel 5

Laravel RoutingRouting is the process of taking a URI endpoint (that part of the URI which comes after the base URL) and decomposing it into parameters to determine which module, controller, and action of that controller should receive the request.

http://laravel.com/docs/5.0/routing

Route::get('/', function(){ return 'Hello World';});

Route::post('foo/bar', function(){ return 'Hello World';});

Route::any('foo', function(){ return 'Hello World';});

Route::match(['put', 'delete'], '/', function() { return 'Hello World';});

Page 13: Getting to know Laravel 5

HTTP METHODS• GET : used to retrieve (or read) a representation of a

resource. Return 200 OK or 404 NOT FOUND or 400 BAD REQUEST

• POST : used to create new resources. Return 201 OK or 404 NOT FOUND

• PUT : used to update existing resource. Return 200 OK or 204 NO CONTENT or 404 NOT FOUND

• DELETE : used to delete existing resource. Return 200 OK or 404 NOT FOUND

http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

Page 14: Getting to know Laravel 5

CODING TIME!• Open app/Http/routes.php

• Insert couple of routes

Route::get('tesget', function(){ return 'Hello World';});

Route::post('tespost', function(){ return 'Hello World';});

Route::any('tesany', function(){ return 'Hello World';});

Route::match(['put', 'delete'], 'tesmatch', function() { return 'Hello World';});

Page 15: Getting to know Laravel 5

CODING TIME!

• Insert -> Route::resource(‘welcome’, ‘WelcomeController');

• Go to terminal / command line.

• Type php artisan route:list

Page 16: Getting to know Laravel 5

Laravel ControllerInstead of defining all of your request handling logic in a single routes.php file, you may wish to organize this behavior using Controller classes. Controllers can group related HTTP request handling logic into a class. Controllers are typically stored in the app/Http/Controllers directory.

http://laravel.com/docs/5.0/controllers

Page 17: Getting to know Laravel 5

CODING TIME!

• go to app/Http/Controller

• create new Controller class (DummyController.php)

• go to app/Http/routes.php

• insert -> Route::get(‘dummy’, ‘DummyController@index‘);

Page 18: Getting to know Laravel 5

CODING TIME!

• php artisan make:controller Dummy2Controller

• go to app/Http/routes.php

• insert -> Route::resource(‘dummy2’, ‘Dummy2Controller');

Page 19: Getting to know Laravel 5

Laravel ModelA Model should contain all of the Business Logic of your application. Or in other words, how the application interacts with the database.

http://laravel.com/docs/5.0/database

The database configuration file is config/database.php

DB::select('select * from users where id = :id', ['id' => 1]);

DB::insert('insert into users (id, name) values (?, ?)', [1, 'Dayle']);

DB::update('update users set votes = 100 where name = ?', ['John']);

DB::delete('delete from users');

DB::statement('drop table users');

Page 20: Getting to know Laravel 5

Query BuilderThe database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application, and works on all supported database systems.

http://laravel.com/docs/5.0/queries

DB::table('users')->get();

DB::table('users')->insert( ['email' => '[email protected]', 'votes' => 0]);

DB::table('users') ->where('id', 1) ->update(['votes' => 1]);

DB::table('users')->where('votes', '<', 100)->delete();

Page 21: Getting to know Laravel 5

Eloquent ORMThe Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table.

http://laravel.com/docs/5.0/eloquent

class User extends Model {}

php artisan make:model User

class User extends Model { protected $table = 'my_users';}

$users = User::all();

$model = User::where('votes', '>', 100)->firstOrFail();

Page 22: Getting to know Laravel 5

Schema BuilderThe Laravel Schema class provides a database agnostic way of manipulating tables. It works well with all of the databases supported by Laravel, and has a unified API across all of these systems.

http://laravel.com/docs/5.0/schema

Schema::create('users', function($table){ $table->increments('id');});

Schema::rename($from, $to);

Schema::drop('users');

Page 23: Getting to know Laravel 5

Migrations & SeedingMigrations are a type of version control for your database. They allow a team to modify the database schema and stay up to date on the current schema state. Migrations are typically paired with the Schema Builder to easily manage your application's schema.

http://laravel.com/docs/5.0/migrations

php artisan make:migration create_users_table

class UserTableSeeder extends Seeder { public function run() { DB::table('users')->delete(); User::create(['email' => '[email protected]']); }}

php artisan db:seed

php artisan migrate --force

Page 24: Getting to know Laravel 5

CODING TIME!• go to env.example, rename it to .env

• set DB_HOST=localhost

• set DB_DATABASE=yourdbname

• set DB_USERNAME=yourdbusername

• set DB_PASSWORD=yourdbpassword

Page 25: Getting to know Laravel 5

CODING TIME!run php artisan make:migration create_sample_table

run php artisan migrate

Page 26: Getting to know Laravel 5

CODING TIME!create SampleTableSeeder.php

run php artisan db:seedrun composer dump-autoload

Page 27: Getting to know Laravel 5

CODING TIME!run php artisan make:model Samplerun composer migrateedit app/Sample.php

Page 28: Getting to know Laravel 5

Laravel ViewViews contain the HTML served by your application, and serve as a convenient method of separating your controller and domain logic from your presentation logic. Views are stored in the resources/views directory.

http://laravel.com/docs/5.0/views

Page 29: Getting to know Laravel 5

CODING TIME!create resources/views/sample.php

create app/Http/controllers/SampleController.php

register the route in app/Http/routes.php