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
540
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
360
jQuery vs AngularJS, dochi?
jaceju
20
2.9k
Begining Composer
jaceju
24
5k
Other Decks in Programming
See All in Programming
CSC509 Lecture 03
javiergs
PRO
0
140
ROS 2のZenoh対応とZenohのROS 2対応
takasehideki
2
310
Новый уровень ML-персонализации Lamoda: Как мы усилили ее в каталоге и перенесли на другие продукты
lamodatech
0
210
【YAPC::Hakodate 2024】TypeScriptエンジニアが感じたPerlのここが面白い
kimitashoichi
1
270
デバッグの話 / Debugging for Beginners
kaityo256
PRO
8
620
コードレビューと私の過去と未来
jxmtst
0
290
ビット演算の話 / Let's play with bit operations
kaityo256
PRO
4
180
VS Code extension: ドラッグ&ドロップでファイルを並び替える
ttrace
0
170
GrafanaのHTTP API を眺めてみよう
rinchoku
0
170
2024-10-02 dev2next - Application Observability like you've never heard before
jonatan_ivanov
0
180
実践サーバーレスパフォーマンスチューニング ~その実力に迫る~ / Practical Serverless Performance Tuning ~A Close Look at its Power~
seike460
PRO
2
170
文化が生産性を作る
jimpei
3
570
Featured
See All Featured
Designing the Hi-DPI Web
ddemaree
280
34k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Creatively Recalculating Your Daily Design Routine
revolveconf
217
12k
Agile that works and the tools we love
rasmusluckow
327
21k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
664
120k
Building Adaptive Systems
keathley
38
2.2k
Documentation Writing (for coders)
carmenintech
65
4.4k
Adopting Sorbet at Scale
ufuk
73
9k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.6k
Being A Developer After 40
akosma
84
590k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
1
310
Designing for humans not robots
tammielis
249
25k
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