29
LARAVEL 5

LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

  • Upload
    others

  • View
    23

  • Download
    0

Embed Size (px)

Citation preview

Page 1: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

LARAVEL 5

Page 2: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

What is Laravel?

• PHP Framework

• Most popular on Github

• MVC Architecture

• MIT License

Page 3: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

Why Laravel?

• Composer

• Community

• Code Construction

Page 4: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

@bukhorimuhammad

Less Talking, More Coding

Page 5: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

Requirements

• Composer

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

• PHP 5.4+

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

Page 6: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

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: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

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: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

Fix Folder Permission

• Set storage folder to be writable (777)

• Set vendor folder to be writable (777)

Page 9: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for
Page 10: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

@bukhorimuhammad

Laravel Concepts

Page 11: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

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: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

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: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

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: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

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: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

CODING TIME!

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

• Go to terminal / command line.

• Type php artisan route:list

Page 16: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

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: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

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: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

CODING TIME!

• php artisan make:controller Dummy2Controller

• go to app/Http/routes.php

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

Page 19: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

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: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

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: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

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: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

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: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

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: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

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: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

CODING TIME!run php artisan make:migration create_sample_table

run php artisan migrate

Page 26: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

CODING TIME!create SampleTableSeeder.php

run php artisan db:seedrun composer dump-autoload

Page 27: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

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

Page 28: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

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: LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for

CODING TIME!create resources/views/sample.php

create app/Http/controllers/SampleController.php

register the route in app/Http/routes.php