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
560
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
5.1k
Other Decks in Programming
See All in Programming
flutterkaigi_2024.pdf
kyoheig3
0
150
Micro Frontends Unmasked Opportunities, Challenges, Alternatives
manfredsteyer
PRO
0
110
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
230
C++でシェーダを書く
fadis
6
4.1k
Jakarta EE meets AI
ivargrimstad
0
130
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.7k
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
130
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
170
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
610
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
190
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
200
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
What's in a price? How to price your products and services
michaelherold
243
12k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
430
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
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