Laravel
Composer
nuff said
Database
Schema Builder
Schema::create('posts', function($table)
{
$table->increments('id');
$table->string('title');
$table->string('tagline', 64); // constraint as second argument
$table->text('body');
$table->integer('category_id')
->unsigned() // chain methods for other options
->nullable(); // for optional fields
$table->softDeletes();
$table->timestamps();
});
Migrations
prefixed with timestamp
not indexed (ie. two identical timestamps won't kill everything)
ordered alphabetically
good for teams / feature branching in git
seeding
Fills the database with sample data
Brilliant for development
Even better for Acceptance Testing
Security
User logins, auth checks, secure hashing, password resets
all built in.
Eloquent
Chainable Relationships
function comments()
{
return $this->hasMany('Comment');
}
$post->comments()->where('user_id', $user->id)->get();
Dynamic properties:
$post->comments;
Eager Loading
function friends()
{
return $this->belongsToMany('User');
}
User::with('friends')->get();
Polymorphic Relationships
An Eloquent Model can Belong To more than one type of Model using the same key
Blade
SYNTAX
PHP
<a href="<?php echo $post->link; ?>">
<?php echo $post->title; ?>
</a>
Blade
<a href="{{ $post->link }}">
{{ $post->title }}
</a>
BLADE
Control Statements
@if( $a > 3 )
{{ $a }} @endif
Partials
@section('scripts')
<script>alert('woop');</script>
@stop
@yield('scripts')
The Downside
But...
Caching
Blade gets cached
queueing
You can distribute tasks, or execute them asynchronously
Has drivers for:
- Amazon SQS
- Beanstalkd
- IronMQ
Routing
Routes
Route::get('/posts/{post}', function($post)
{
return "Post title: $post!";
});
Model Binding
Route::bind('post-slug', function($slug) {
return Post::where('slug', $slug)
->firstOrFail();
})
Route::model('post', 'Post');
Model Binding
Route::get('posts/{post-slug}', function($post) {
return View::make('blog.post')
->with('post', $post);
});
Route Groups
Route::group(['prefix' => 'post'], function() {
Route::get('{post-slug}', function($post) { return 'the post'; });
Route::get('{post-slug}/edit', function($post) { return 'edit post'; });
Route::post('{post-slug}/edit', function($post) {
// Validate
// Save
return Redirect::route('blog.post');
});
});
Named Routes
Route::get('posts', ['as' => 'posts.list', function()
{
return 'list of posts';
}]);
Redirect::route('posts.list);
URL::route('posts.list');
URL::route('posts.show', ['post-slug' => $post->slug]);
Controllers
Route::get('home', ['as' => 'home', 'uses' => 'SiteController@index']);
Route::controller('/', 'SiteController');
/* In SiteController.php */
public function getIndex() { return ''; }
public function postIndex() { return ''; }
Resource Controllers
Route::resource('post', 'PostController');
Filters
Route::group(['prefix' => 'admin', 'before' => 'auth|is:admin'], function() {
Route::get('', function() { return ''; })
});
Route::filter('is', function($level)
{
if ( ! Auth::user()->is($level) )
{
return Redirect::route('home');
}
});
The Symfony console component
(artisan)
The Artisan Command Skeleton
Every command has a description, defined inputs, etc.
Colours!!!!
Formatting Presets
$this->info('You did something');
$this->error('Shit gone crazy');
The symfony terminal markup language
$this->output('<fg="green">foo</fg="green" ');
Custom macros
$style = new OutputFormatterStyle('red', 'yellow', array('bold', 'blink'));
$this->output->getFormatter()->setStyle('fire', $style);
$this->output->writeln('<fire>foo</fire>');
INVERSION OF CONTROL
& The facade pattern
Code Happy
To generate a url to a blog post
CodeIgniter
$this->load->helper('url');
site_url(array('blog', 'posts', $post->slug));
Fuel
Uri::create("blog/posts/{$post->slug}");
Laravel
URL::to('blog/posts/new-post);
URL::route('blog.post', ['slug' => $post->slug]);
The IOC Container
Based on Symfony's IOC Container
It resolves dependancies, and instantiations
The Facade Pattern
Config::get();
Fuel
works on static properties and methods
Laravel
IOC resolves it to a single shared instance
replaces the need for the singleton pattern:
$this->config &= Config::getInstance()
Testing
Unit Testing
IOC Container
Dependancy Injection
Mockery
Acceptance Testing
Forms user stories
Conclusion
It encourages better coding
why laravel
By Stef Horner
why laravel
- 711