LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the...
Transcript of LARAVEL 5 - maniganesh.files.wordpress.com · Laravel Artisan Artisan is the name of the...
LARAVEL 5
What is Laravel?
• PHP Framework
• Most popular on Github
• MVC Architecture
• MIT License
Why Laravel?
• Composer
• Community
• Code Construction
@bukhorimuhammad
Less Talking, More Coding
Requirements
• Composer
• Local server (with XAMPP, WAMP, MAMP, etc)
• PHP 5.4+
• Mcrypt, OpenSSL, Mbstring, Tokenizer & PHP JSON extension
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
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
Fix Folder Permission
• Set storage folder to be writable (777)
• Set vendor folder to be writable (777)
@bukhorimuhammad
Laravel Concepts
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
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';});
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
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';});
CODING TIME!
• Insert -> Route::resource(‘welcome’, ‘WelcomeController');
• Go to terminal / command line.
• Type php artisan route:list
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
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‘);
CODING TIME!
• php artisan make:controller Dummy2Controller
• go to app/Http/routes.php
• insert -> Route::resource(‘dummy2’, ‘Dummy2Controller');
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');
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();
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();
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');
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
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
CODING TIME!run php artisan make:migration create_sample_table
run php artisan migrate
CODING TIME!create SampleTableSeeder.php
run php artisan db:seedrun composer dump-autoload
CODING TIME!run php artisan make:model Samplerun composer migrateedit app/Sample.php
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
CODING TIME!create resources/views/sample.php
create app/Http/controllers/SampleController.php
register the route in app/Http/routes.php