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
360
ng-conf_2014
jaceju
2
980
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
テスト自動化失敗から再挑戦しチームにオーナーシップを委譲した話/STAC2024 macho
ma_cho29
1
1.3k
フロントエンドのディレクトリ構成どうしてる? Feature-Sliced Design 導入体験談
osakatechlab
8
4.1k
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1k
DevFest Tokyo 2025 - Flutter のアプリアーキテクチャ現在地点
wasabeef
5
890
競技プログラミングへのお誘い@阪大BOOSTセミナー
kotamanegi
0
350
良いユニットテストを書こう
mototakatsu
4
1.5k
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
130
As an Engineers, let's build the CRM system via LINE Official Account 2.0
clonn
1
670
testcontainers のススメ
sgash708
1
120
14 Years of iOS: Lessons and Key Points
seyfoyun
1
770
あれやってみてー駆動から成長を加速させる / areyattemite-driven
nashiusagi
1
200
Webエンジニア主体のモバイルチームの 生産性を高く保つためにやったこと
igreenwood
0
330
Featured
See All Featured
Building Adaptive Systems
keathley
38
2.3k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
Embracing the Ebb and Flow
colly
84
4.5k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
A Tale of Four Properties
chriscoyier
157
23k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
Gamification - CAS2011
davidbonilla
80
5.1k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
Building Applications with DynamoDB
mza
91
6.1k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
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