This document provides an overview of Laravel, a popular PHP framework. It discusses what Laravel is, why it is popular, and some of its core components like routing, controllers, models, migrations and views. Key points include: Laravel uses MVC architecture and is composer-based; it includes features like routing, controllers, Eloquent ORM, schema builder, migrations and seeding to interact with databases, and blade templating for views. Requirements to use Laravel are PHP 5.4+, composer, and database extensions like MySQL.
6. COMPOSER
Composer 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
11. Laravel Artisan
Artisan 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
12. Laravel Routing
Routing 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';
});
13. 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
15. CODING TIME!
• Insert -> Route::resource(‘welcome’,
‘WelcomeController');
• Go to terminal / command line.
• Type php artisan route:list
16. Laravel Controller
Instead 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
17. 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‘);
18. CODING TIME!
• php artisan make:controller Dummy2Controller
• go to app/Http/routes.php
• insert -> Route::resource(‘dummy2’,
‘Dummy2Controller');
19. Laravel Model
A 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');
20. Query Builder
The 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' => 'john@example.com', 'votes' => 0]
);
DB::table('users')
->where('id', 1)
->update(['votes' => 1]);
DB::table('users')->where('votes', '<', 100)->delete();
21. Eloquent ORM
The 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();
22. Schema Builder
The 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');
23. Migrations & Seeding
Migrations 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' => 'foo@bar.com']);
}
}
php artisan db:seed
php artisan migrate --force
24. 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
25. CODING TIME!
run php artisan make:migration create_sample_table
run php artisan migrate
27. CODING TIME!
run php artisan make:model Sample
run composer migrate
edit app/Sample.php
28. Laravel View
Views 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