Clean and classy PHP framework
Presentation by Maks
class Company extends Eloquent {}
$company = Company::create(array( 'address' => '155 Harbor Blvd', 'city' => 'Fullerton' ));
$company = Company::find(1); echo $company->address; // 155 Harbor Blvd
$company = Company::where('city', "Fullerton")->first();
$companies = Company::orderBy('city', 'desc')->take(10)->get();
$companies = Company::orderBy('city', 'desc')->paginate(10);
class Company extends Eloquent { public function salesperson() { return $this->hasOne('Salesperson'); } }
$salesperson = Company::find(1)->salesperson->name;
class Company extends Eloquent { public function customers() { return $this->hasMany('Customer'); } }
$customers = Company::find(1)->customers;
class User extends Eloquent { public function scopePopular($query) { return $query->where('votes', '>', 100); } public function scopeWomen($query) { return $query->whereGender('W'); } }
// Using the scope
$popularWomen = User::popular()->women()->orderBy('created_at')->get();
Route::get('/', function() { return "Hello PHP!"; }); Route::post('user', function() { // });
Passing parameters:
Route::get('user/{id}', function($id) { return 'User '.$id; });
Route Constraints:
Route::get('user/{name}', function($name) { // })->where('name', '[A-Za-z]+');
Controller Routing
Route::controller('company');
OR
Route::get('admin','AdminController@getIndex');
Route filters
Route::get('create', array('before' => 'auth|csrf', function() { // }));
$credentials = array( 'username' => 'example@gmail.com', 'password' => 'secret' ); if (Auth::attempt($credentials)) { return Redirect::to('user/profile'); }
Get user's info : $email = Auth::user()->email;
Logout: Auth::logout();
Session::put('key', 'value'); or for next request only Session::flash('key', 'value'); $value = Session::get('key'); if (Session::has('cart')) { // } Session::forget('key'); Session::flush();
@if ($tags)
<p class="lead">Tags matching the search:</p> <ul class='nav nav-list'> @foreach ($tags as $tag) <li> <a href="{{ url('tags/'.$tag->slug) }}"> <span class="label">{{$tag->count}} snipps</span> {{$tag->name}} </a> </li> @endforeach @endif
HTML::script('js/scrollTo.js'); // <script src="http://localhost:8000/js/scrollTo.js"></script> HTML::style('css/common.css'); // <link media="all" type="text/css" rel="stylesheet" href="http://localhost:8000/css/common.css">
Form helpers:
Form::open(); Form::text('username'); Form::password('password'); Form::checkbox('name', 'value', true); Form::file('image'); Form::select('size', array('L' => 'Large', 'S' => 'Small')); Form::submit('Click Me!'); Form::close();
bootstrap/start.php:
$env = $app->detectEnvironment(array( 'local' => array('http://localhost*', '*.dev'), 'staging' => array('http://dev.mywebsite.com*'), 'production' => array('http://mywebsite.com*'), ));
Then add environment-specific config files under
app/config
app - config - staging - database.php - session.php - production - database.php ...
Done!
$input = Input::all(); $rules = array( 'name' => 'required|min:3|max:32|alpha', 'age' => 'required|integer', 'image'=> 'image|max:1500', 'password' => 'confirmed'
); $v = Validator::make($input, $rules); if( $v->fails() ) { // code for validation failure :( } else { // code for validation success! }
Get list of all commands:
php artisan list
key:generate Generate a secure application key.
# Seeding of the DB db:seed
# Migrations migrate:install Create the Laravel migration table. migrate:make Create a migration. migrate Run outstanding migrations. migrate:rollback Roll back the most recent migration. migrate:reset Roll back all migrations.