Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Laravel

Laravel presentation for the GTA PHP User Group on November 6th, 2012
by

Gary Saunders

on 7 November 2012

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Laravel

Gary Saunders
gary@powrit.com What have I done? About a decade in IT Infrastructure
EDS (now HP) and IBM Until about a year or so ago... Microsoft and Linux
HA / Virtualization / Design / Consulting / Support VMWare (yummy) Surfed a lot of expertsexchange.com
... and eventid.net and Google Hardware
Servers / Storage / Tape / Other fun things Sir Sandford Flemming
Computer Programmer / Analyst ... and even before that. (while at EDS)
Developed knowledgebase for helpdesk Professional Google'r Music
Fast Tracker II, guitar / vocals in various garage bands Made a lot of cool support tools Returned to my roots
Started programming professionally again (mostly web) > a year so ago Quit my job
wife !== happy Surfed a lot of StackOverflow and Reddit Music
Cubase / Piano / Guitar / Audio gear and
Recording studio in my basement Had another kid
Still performing root cause analysis A few of my favourite things Because I like $('.shiny') objects Because clients think I'm a
Designer Runs anywhere and
everywhere LAMP Linux
Apache
MySQL
PHP Because configuring servers is a pain in the arse Because all the cool hosting providers are integrating it into their deployment process Git Because it is awesome Speaking of Laravel... Elastic Beanstalk
PagodaBox
BeanstalkApp PHP 5.3
MVC Framework
Extensive use of closures
Written by 1 (smart) guy - Taylor Otwell
Active online community
Sponsored by "UserScape"
Currently at version 3.2
Version 4 (Composer based) coming next year
MIT License! Why use a Framework? Agility
Consistency
Manageability
Scalability
Portability
Agnostic by Nature
Features What does "Laravel" mean? It means "laravel" was an available
7-letter domain name that was fun
to say, kind of sounds like the name
of a pin-up girl you'd see on the side
of a WWII bomber. Also the logo is a squirrel...

(unofficially) Agility Configuration for Infrastructure and Environments It has to give you something. It must provide additional value. It must be intuitive. It must justify its existence. Local / dev
http://localhost

Staging
http://staging.myapp.com

Production
http://myapp.com

WhiteLabel
http://betterthanyours.com Different URLs, DB Credentials, Session configuration, cache configuration, even different DB technology!

E.g. - SQLite on localhost, MySQL in Production $environments = array(
'local' => array( 'http://localhost*', '*.dev' ),
'gary' => array( 'http://*gary.dev' ),
'staging' => array( 'http://staging.myapp.com' ),
'production' => array( 'http://*myapp.com' ),
); In Laravel you don't have to setup any kind of
environment variable or perform any O/S task
just to assign a set of configuration params. Laravel will match the URL of the incoming request request and load the relevant configuration from the "gary" sub-folder. return array(
'default' => 'mysql',
'connections' => array(
'sqlite' => array(
'driver' => 'sqlite',
'database' => 'application',
'prefix' => '',
),
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
),
),
); And what does a laravel config file look like? An example "database.php" file: What if your app / site gets Tweeted by
Justin Bieber tomorrow and your system gets HAMMERED by new visitors? Add a caching layer to your application. return array(
'driver' => 'memcached',
'key' => 'laravel',
'memcached' => array(
array(
'host' => '127.0.0.1',
'port' => 11211,
'weight' => 100
),
),
); ( assumes you have used the Blade template
engine for views ) Are you storing your sessions in a database to maintain persistence across multiple web hosts in a cluster? return array(
/*
|
| Drivers: 'cookie', 'file', 'database',
| 'memcached', 'apc', 'redis'.
|
*/
'driver' => 'database',
'table' => 'sessions',
); Example session.php Getting from Request to Response Extremely High Level Overview in Laravel 1. Setup a route
2. Return something

3. Forward to controller method Setup a Route --- or --- Route::any('/user/home',function(){
return Response::make('Hello World');
}); this is returning something URI to process - this route will be processed for http://site.com/user/home Route to a specific method Route::any('/user/home',array(
'use' => 'controller@method'
)); The controller method will be
responsible for returning a response URI to process - this route will be processed for http://site.com/user/home Use Parameters from the URI Route::any('/articles/(:num)/edit',function($article_id){
$article = Article::find($article_id);
return View::make('article.edit')
->with('article',$article);
}); URI to process - this route will be processed for http://site.com/articles/<any numeric value>/edit A piece of the URL to send to the controller / function as a parameter Class MyName_Controller extends Base_Controller {
public function action_say($myName = null)
{
return Response::make(
($myName !== null) ?
"Hello $myName!" :
"What's your name?"
);
}
} RESTful Controllers Class MyName_Controller extends Base_Controller {
public $restful = true;
public function get_myName()
{
return Response::make(
Session::get('myname','')
);
}
public function post_myName()
{
$myName = Input::get('myName');
Session::put('myName',$myName);
return Response::make(
"I will remember you, " . $myName
);
}
} Prefix controller methods
with their associated HTTP
action verb.

GET, POST, PUT and DELETE supported. Controllers Code your methods RESTfully, or not 1. Make a new controller
2. Define $restful = true

3. Just make regular methods --- or --- Making RESTful APIs is funner and easier
than ever b4! Automagically route to all controllers Route::controller(Controller::detect()); Show the world what your controllers are made of! And be old school! Automatically creates routes for all of your controller methods at run-time. e.g. - The "myName" method on the "myName_Controller" controller will automatically be routed as... http://site.com/myname/myname ... or maybe just one Route::controller('MyName'); Achievement unlocked: quasi-old school! Automatically creates routes for all of the MyName controller methods at run-time. e.g. - The "myName" method on the "myName_Controller" controller will automatically be routed as... http://site.com/myname/myname I can has Controllers? MVC Yes. A regular ol' method hanging out in a controller as notated by the "action_" prefix. This method does not RESTful. VERY useful when porting legacy controllers
from other frameworks. ... or completely abstract the URI from the underlying method Route::any('/something',array(
'as' => 'everything something',
'use' => 'myname@myname',
)); Yep. Routes any type of HTTP request to /something to the "myname" method of the "myname" controller. Named routing: 'as' parameter can be used later when generating URLs. Class User extends Eloquent {

} Naming Conventions Models Because writing SQL sucks. Laravel's ORM is called "Eloquent" Table "Users" = a model called "User" Everything is chainable $users = User::where('lastname','like','S%'); Find all users whose last name starts with "S"... And who also like cheese. You don't even have to define setters and getters! class User extends Eloquent {
public function set_password( $password )
{
$this->set_attribute( 'password', Hash::make( $password ) );
}
} ... but you can, and they come in handy. Models like relationships class Model extends Eloquent {

public function boyfriend()
{
return $this->has_one( 'BoyFriend' );
}

} Eloquent ORM Provides a base class to extend models from Based on PHP ActiveRecord which I understand is a rip-off of RoR's ORM Uses PDO and prepared statements, allows you to access tables and rows as {objects} Helps you develop faster, more consistently and with improved security An Example Model Class file (don't blink) Pay attention to table naming conventions, table names are plural, model names are singular. $users->where('fav_food','=','cheese'); ... or have logged in today and wear size 11 shoes. $users->or_where(function($query){
$query->where('last_login','>=,date('Y-m-d'));
$query->where('shoe_size','=',11);
}); $user->password = "ilovepokemon"; class User extends Eloquent {
} $user->password = Hash::make( "ilovepokemon" ); class BoyFriend extends Eloquent {

public function girlfriend()
{
return $this->belongs_to( 'Model' );
}

} $model = Model::where_firstname( 'Naomi' );
$model_boyfriend = $model->boyfriend; Models Table: id - firstname - lastname - measurements Boyfriends Table: id - firstname - lastname - model_id fk Some Models REALLY like relationships class PromiscuousGirl extends Eloquent {

public function boyfriends()
{
return $this->has_many( 'BoyFriend' );
}

} class BoyFriend extends Eloquent {

public function girlfriend()
{
return $this->belongs_to( 'PromiscuousGirl' );
}

} $model = Model::where_firstname( 'Naomi' );
$boyfriends = $model->boyfriends();

foreach($boyfriends->get() as $boyfriend)
{
$boyfriend->duped = true;
} PromiscuousGirls Table (sold out): id - firstname - lastname - measurements Boyfriends Table: id - firstname - lastname - promiscuousgirl_id fk Some Girls like many boys and Vice Versa class GirlFriend extends Eloquent {

public function boyfriends()
{
return $this->has_many_and_belongs_to( 'BoyFriend' );
}

} class BoyFriend extends Eloquent {

public function girlfriends()
{
return $this->has_many_and_belongs_to( 'GirlFriend' );
}

public function score($girlfriend_id)
{
$this->girlfriends()->attach( $girlfriend_id );
}
} $model = Model::where_firstname( 'Naomi' );
$boyfriends = $model->boyfriends();

foreach($boyfriends->get() as $boyfriend)
{
$girls = $boyfriend->girlfriends()->count();
} GirlFriends Table: id - firstname - lastname Boyfriends Table: id - firstname - lastname boyfriend_girlfriend (pivot) Table: id - girlfriend_id - boyfriend_id Views Blade makes views less frustrating. Example of a view. It's just PHP and HTML <html>
<body>
<h1><?php echo $pageHeader; ?></h1>
<h2>Zend Framework Pricing</h2>
<table>
<thead>
<tr><th>Regular</th><th>Extra-Large</th></tr>
</thead>
<tbody>
<tr><td>$100/mth</td><td>$100,000/mth</td></tr>
<tr><td colspan="2">** Additional licensing and maintenance fees apply</td></tr>
</tbody>
</table>
</body> Example of a view using Blade Templating. ... unless it's not <html>
<body>
<h1>{{ $pageHeader }}</h1>
<h2>Zend Framework Pricing</h2>
<table>
<thead>
<tr><th>Regular</th><th>Extra-Large</th></tr>
</thead>
<tbody>
<tr><td>$100/mth</td><td>$100,000/mth</td></tr>
<tr><td colspan="2">** Additional licensing and maintenance fees apply</td></tr>
</tbody>
</table>
</body> There are HTML macros <html>
<body>
<h1>{{ $pageHeader }}</h1>
{{ Form::open(URL::to_route('something everywhere')) }}
{{ Form::token() }}
{{ Form::text( 'email', 'user@domain.com' ) }}
{{ Form::submit() }}
{{ Form::close() }}
</body> You can make your own macros too You can pass data into views Route::('/videos/top100',function(){
$videos = Video::top100();
return View::make('videos.top100')
->with('top100',$videos)
->render();
}); <html>
<body>
<ul>
@foreach($top100 as $video)
<li>{{ $video->name }}</li>
@endforeach
</ul>
</body> <- Example view using Blade /views/videos/top100.blade.php You can generate URLs to named routes <html>
<body>
<ul>
@foreach($top100 as $video)
<li><a href="{{ URL::to_route('video details',$video->id) }}">{{ $video->name }}</a></li>
@endforeach
</ul>
</body> WITH parameters! Route::('/videos/details/(:num)',array(
'as' => 'video details',
function($video_id){
$video = Video::find($video_id);
return View::make('videos.details')
->with('video',$video)
->render();
})); -- Makes a URL like /videos/details/1 Example of a route to receive that link Note: 'as' defines the name of the route for the URL::to_route function You can even overwrite View rendering! With classes and even bundles like Twig! You can even sense That I was rushing to finish assembling this presentation
and feel the impending unfinished ending! Me Twitter
@musicandpeople @powrit
@codenamegary Laravel Forums
codenamegary GitHub
codenamegary Email
gary@powrit.com Resources Places to go and learn stuff http://www.laravel.com http://forums.laravel.com A course on Udemy http://www.udemy.com/develop-web-apps-with-laravel-php-framework/?couponCode=laravel Books https://leanpub.com/codehappy http://www.packtpub.com/laravel-php-starter/book Tutorials https://www.google.ca/?q=Laravel%20Tutorials People to follow @laravelphp @laravelnews @taylorotwell @phillsparks @shawnmccool @benedmunds @pagodabox @jasonclewis @lie2815 @daylerees @ericlbarnes Mostly people and resources from the core team
Full transcript