LARAVEL 5.1
SESSION 01
Muhammad Rizwan Arshad
Principal Software Engineer, Nextbridge.
July 27, 2015
Installation
Server Requirements
- PHP >= 5.5.9
- OpenSSL PHP Extension
- PDO PHP Extension
- Mbstring PHP Extension
- Tokenizer PHP Extension
Via Composer Create-Project
# composer
create-project
laravel/laravel
{directory_name}
{version}
Configuration
- Config Directory
- Permissionis: storage and bootstrap/cache
- Additonal: app/config.php
- Pretty URL (Apache and Nginx)
- Environment Configuration: Dot files
- Application Environment:
- $environment = App::environment();
- if (App::environment('local', 'staging'))
- Config Values:
- $value = config('app.timezone');
- config(['app.timezone' => 'America/Chicago']);
HTTP Routing
Basic Routing app/Http/routes.php
Route::get('/', function () {
return 'Hello World';
});
Route::post('foo/bar', function () {
return 'Hello World';
});
Route::put('foo/bar', function () {
//
});
Route::delete('foo/bar', function () {
//
});
Multiple Verbs
Route::match(['get', 'post'], '/', function () {
return 'Hello World';
});
Route::any('foo', function () {
return 'Hello World';
});
$url = url('foo');
Required Parameters
Route::get('user/{id}', function ($id) { return 'User '.$id; });
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) { // });
Regular Expression Constraints
Route::get('user/{name}', function ($name) { // })->where('name', '[A-Za-z]+'); Route::get('user/{id}', function ($id) { // })->where('id', '[0-9]+'); Route::get('user/{id}/{name}', function ($id, $name) { // })->where(['id' => '[0-9]+', 'name' => '[a-z]+']);
Named Routes
Route::get('user/profile', ['as' => 'profile', function () { // }]);
Route::get('user/profile', [ 'as' => 'profile', 'uses' => 'UserController@showProfile' ]);
Route Groups &
Named Routes
Route::group(['as' => 'admin::'], function () {
Route::get('dashboard', ['as' => 'dashboard', function () {
// Route named "admin::dashboard"
}]);
});
Generating URLs To
Named Routes
$url = route('profile'); $redirect = redirect()->route('profile');
Route::get('user/{id}/profile', ['as' => 'profile', function ($id) { // }]); $url = route('profile', ['id' => 1]);
Route Prefixes
Route::group(['prefix' => 'admin'], function () {
Route::get('users', function () {
// Matches The "/admin/users" URL
});
});
Route::group(['prefix' => 'accounts/{account_id}'], function () {
Route::get('detail', function ($account_id) {
// Matches The accounts/{account_id}/detail URL
});
});
CSRF Protection
<?php echo csrf_field(); ?>
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
{!! csrf_field() !!}
Form Method Spoofing
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
Throwing 404 Errors
abort(404);
Your Feedback !
Laravel 5.1 - Session 01
By gr8rizwan
Laravel 5.1 - Session 01
- 1,098