Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Laravel Framework Modern Website Building
Search
大澤木小鐵
November 03, 2012
Programming
9
1.6k
Laravel Framework Modern Website Building
大澤木小鐵
November 03, 2012
Tweet
Share
More Decks by 大澤木小鐵
See All by 大澤木小鐵
Effective Unit Testing
jaceju
3
550
JSConf Asia 2014 Sessions
jaceju
4
390
What happens in Laravel 4 bootstraping
jaceju
9
550
Deal with Laravel assets by Bower & Gulp
jaceju
30
1.9k
Leaning MVC By Example
jaceju
0
350
ng-conf_2014
jaceju
2
970
The Power of JavaScript in JSConf.Asia 2013
jaceju
5
370
jQuery vs AngularJS, dochi?
jaceju
20
2.9k
Begining Composer
jaceju
24
5k
Other Decks in Programming
See All in Programming
現場で役立つモデリング 超入門
masuda220
PRO
13
2.9k
Universal Linksの実装方法と陥りがちな罠
kaitokudou
1
220
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
330
詳細解説! ArrayListの仕組みと実装
yujisoftware
0
480
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
130
WEBエンジニア向けAI活用入門
sutetotanuki
0
300
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.1k
推し活の ハイトラフィックに立ち向かう Railsとアーキテクチャ - Kaigi on Rails 2024
falcon8823
6
2.2k
Googleのテストサイズを活用したテスト環境の構築
toms74209200
0
270
Piniaの現状と今後
waka292
5
1.5k
[PyCon Korea 2024 Keynote] 커뮤니티와 파이썬, 그리고 우리
beomi
0
110
カスタムしながら理解するGraphQL Connection
yanagii
1
1.2k
Featured
See All Featured
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
Rails Girls Zürich Keynote
gr2m
93
13k
Gamification - CAS2011
davidbonilla
80
5k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.2k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Designing for humans not robots
tammielis
249
25k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
167
49k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
41
2.1k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.8k
Fashionably flexible responsive web design (full day workshop)
malarkey
404
65k
Fireside Chat
paigeccino
32
3k
Transcript
Modern Website Building Laravel Framework
Jace Ju 大澤木小鐵 http://plurk.com/jaceju http://twitter.com/jaceju http://www.jaceju.net
Why Laravel ? 0
What Is Web Framework 把麻煩的事簡單化 把重複的事自動化
The Basic Features • 有效率地處理使⽤用者的請求。 • ⽤用更抽象化的⽅方式處理資料內容。 • 有效管理與整合前端⽂文件。 •
快速回應各種不同格式的內容。 • 提供基本的安全防護。 • 完整的⽂文件。 • 易於擴充與佈署。
How to choose ?
About Laravel Framework • Taylor Otwell 於 2011 年 4
月發表 • MIT License • 基於 PHP 5.3 的 MVC Framework • 豐富⽽而完整的⽂文件 • 易懂的 API ⽤用法 • 易於擴充新功能 http://laravel.com/
Classy & Fluent Interface return Redirect::to('login') ->with_input(); $comments = Post::find(1)->comments;
Asset::container('footer') ->add('example', 'js/example.js'); $url = URL::to_secure('some/uri');
Environment 1
Environment • 開發 / 本機 • 測試 • 正式
Configurations • 資料庫主機 IP • 網址 • 除錯策略 • ...
paths.php $environments = array( 'production' => array( 'http://www.example.com*'), 'testing' =>
array( 'http://test.example.com*'), 'local' => array( 'http://localhost*', '*.dev'), );
application/config application/config/production/*.php application/config/testing/*.php application/config/local/*.php application/config/*.php http://laravel.com/docs/install#environments
Command Line 2
artisan http://laravel.com/docs/artisan/tasks
Task php artisan migration php artisan key:generate php artisan test
Custom Task // application/tasks/notify.php class Notify_Task { // php artisan
notify taylor public function run($arguments) { // Do awesome notifying... } // php artisan notify:urgent public function urgent($arguments) { // This is urgent! } }
Migration 3
• 快速建⽴立正式與測試資料庫 • 管理 SQL 檔案 • 還原成前⼀一版的 Schema •
切換 DBMS Problems
Migration php artisan migrate:install php artisan migrate:make create_users_table
Migration class Create_Users_Table { public function up() { Schema::table('users', function($table)
{ $table->create(); $table->increments('id'); $table->string('username'); $table->string('email')->unique(); $table->timestamps(); }); } public function down() { Schema::drop('users'); } }
Migration php artisan migrate // 執⾏行 migration php artisan migrate:rollback
// 撤消上⼀一步 http://laravel.com/docs/database/migrations
Autoloading 4
• require • require_once • autoload (PHP 5) Get Classes
Autoloader::directories Autoloader::directories(array( path('app') . 'entities', path('app') . 'repositories', )); http://laravel.com/docs/loading
Autoloader::map Autoloader::map(array( 'User' => path('app').'models/user.php', 'Contact' => path('app').'models/contact.php', ));
Autoloader::namespaces Autoloader::namespaces(array( 'Doctrine' => path('libraries') . 'Doctrine', 'Zend' => path('libraries')
. 'Zend', )); https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
Autoloader::underscored Autoloader::underscored(array( 'Swift' => path('libraries') . 'SwiftMailer', ));
Alias // application/config/application.php array( 'aliases' => array( 'Asset' => 'Laravel\\Asset',
'Autoloader' => 'Laravel\\Autoloader', // ... ), );
Routing 5
User-Friendly URLs http://www.example.com/2012/08/first-article http://www.example.com/my/articles
RESTful URLs http://www.example.com/category/2 http://www.example.com/category/item/5
Event-Driven Routing Route::get('/', function() { return "Hello World!"; });
RESTful Routing Route::get('user/(:num)', function($id) { // Get user information });
Route::put('user/(:num)', function($id) { // Update user information });
Controller Routing // application/routes.php Route::controller(Controller::detect());
Input 6
• GET (Query String) • POST (Form) • COOKIE •
FILES HTTP Request
Input & Cookie Input::get($key); // All inputs Input::query($key); // Query
String Input::file($key); // Files Input::json(); $name = Cookie::get('name'); Cookie::put('name', 'Jace', 60);
Redirecting With Old Input return Redirect::to('login') ->with_input(); // Get old
input Input::flash(); $name = Input::old('name'); http://laravel.com/docs/input#redirecting-with-old-input
Validation 7
Do Not Trust User Input Directly
Do Not Trust User Input Directly 在 FB 上拿到的正妹照 千萬不要急著塞到資料褲
庫
Validator $input = Input::all(); $rules = array( 'name' => 'required|max:50',
'email' => 'required|email', ); $validation = Validator::make($input, $rules); if ($validation->fails()) { return $validation->errors; }
Data Abstraction 8
ORM
ORM Orz
Eloquent ORM
Has One class User extends Eloquent { public function phone()
{ return $this->has_one('Phone'); } } $phone = User::find(1)->phone;
Belongs To class Phone extends Eloquent { public function user()
{ return $this->belongs_to('User'); } } echo Phone::find(1)->user->email;
Has Many class Post extends Eloquent { public function comments()
{ return $this ->has_many('Comment'); } } $comments = Post::find(1)->comments; http://laravel.com/docs/database/eloquent
Output 9
View Route::get('/', function() { return View::make('home.index'); }); application/views/home/index.blade.php http://laravel.com/docs/views
View In Controller public function action_index() { return View::make('home.index') ->with('template_var',
'value'); }
JSON Response::json(array('name' => 'Batman')); Response::eloquent(User::find(1));
Assets 10
Assets Dependency jQuery jQuery UI jQuery Form
Global Registration Asset::add('jquery', 'js/jquery.js'); Asset::add('jquery-ui', 'js/jquery-ui.js', 'jquery'); <?php echo Asset::styles();
?> <?php echo Asset::scripts(); ?>
Namespace Registration Asset::container('footer') ->add('example', 'js/example.js'); <?php echo Asset::container('footer') ->scripts(); ?>
http://laravel.com/docs/views/assets
Bundles 11
Bundles In Laravel http://bundles.laravel.com/
Bundle Installation php artisan bundle:install bundle_name // application/bundles.php return array(
'docs' => array('handles' => 'docs'), 'bundle_name' // 註冊 bundle );
Bundle Tasks class Admin_Generate_Task { public function run($arguments) { //
Generate the admin! } } php artisan admin::generate http://laravel.com/docs/bundles
Summary 12
• Form • Errors & Logging • Localization • Authentication
• IoC Container • Unit Testing Other Features http://laravel.com/docs
• Is Good MVC ? • Secure Request • Fast
Response • Simply Data Handling • Extendable The Points
Framework 是⽤用來解決問題 ⽽而不是⽤用來製造問題的
Thank you