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
Vue3の一歩踏み込んだパフォーマンスチューニング2024
hal_spidernight
3
3.1k
プロジェクト新規参入者のリードタイム短縮の観点から見る、品質の高いコードとアーキテクチャを保つメリット
d_endo
1
1k
WEBエンジニア向けAI活用入門
sutetotanuki
0
300
飲食業界向けマルチプロダクトを実現させる開発体制とリアルな現状
hiroya0601
1
390
Server Driven Compose With Firebase
skydoves
0
400
Android 15 でアクションバー表示時にステータスバーが白くなってしまう問題
tonionagauzzi
0
140
Tuning GraphQL on Rails
pyama86
2
1k
色々なIaCツールを実際に触って比較してみる
iriikeita
0
270
Golang と Erlang
taiyow
8
1.9k
[PyCon Korea 2024 Keynote] 커뮤니티와 파이썬, 그리고 우리
beomi
0
110
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
330
僕がつくった48個のWebサービス達
yusukebe
18
17k
Featured
See All Featured
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
355
29k
Gamification - CAS2011
davidbonilla
80
5k
Measuring & Analyzing Core Web Vitals
bluesmoon
1
40
We Have a Design System, Now What?
morganepeng
50
7.2k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
290
It's Worth the Effort
3n
183
27k
How GitHub (no longer) Works
holman
311
140k
Fireside Chat
paigeccino
32
3k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Designing for humans not robots
tammielis
249
25k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
27
1.9k
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