Laravel 5 :(2)Hello World

Laravel 範例實作

此筆記主要紀錄學習以下 Laravel 的範例教學

Laravel 範例

此章節目標

1
2
3
Artisan 指令運用
基本路由設定 / 路由呼叫 controller
controller 傳送參數給 views

Artisan 指令

此指令主要有以下功能

1
2
3
4
5
6
自動建立 controller / models 等程式碼
操作 DB 物件,用來建立或刪除 DB 資料表等
DB seeding
路由
Application 配置
執行 unit tests

在專案底下執行php artisan list會出現以下可用的指令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
C:\xampp\htdocs\larashop>php artisan list
Laravel Framework version 5.3.29

Usage:
command [options] [arguments]

Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--env[=ENV] The environment the command should run under
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for deb
ug

Available commands:
clear-compiled Remove the compiled class file
down Put the application into maintenance mode
env Display the current framework environment
help Displays help for a command
inspire Display an inspiring quote
list Lists commands
migrate Run the database migrations
optimize Optimize the framework for better performance
serve Serve the application on the PHP development server
tinker Interact with your application
up Bring the application out of maintenance mode
app
app:name Set the application namespace
auth
auth:clear-resets Flush expired password reset tokens
cache
cache:clear Flush the application cache
cache:table Create a migration for the cache database table
config
config:cache Create a cache file for faster configuration loading
config:clear Remove the configuration cache file
db
db:seed Seed the database with records
event
event:generate Generate the missing events and listeners based on registration
key
key:generate Set the application key
make
make:auth Scaffold basic login and registration views and routes
make:command Create a new Artisan command
make:controller Create a new controller class
make:event Create a new event class
make:job Create a new job class
make:listener Create a new event listener class
make:mail Create a new email class
make:middleware Create a new middleware class
make:migration Create a new migration file
make:model Create a new Eloquent model class
make:notification Create a new notification class
make:policy Create a new policy class
make:provider Create a new service provider class
make:request Create a new form request class
make:seeder Create a new seeder class
make:test Create a new test class
migrate
migrate:install Create the migration repository
migrate:refresh Reset and re-run all migrations
migrate:reset Rollback all database migrations
migrate:rollback Rollback the last database migration
migrate:status Show the status of each migration
notifications
notifications:table Create a migration for the notifications table
queue
queue:failed List all of the failed queue jobs
queue:failed-table Create a migration for the failed queue jobs database table
queue:flush Flush all of the failed queue jobs
queue:forget Delete a failed queue job
queue:listen Listen to a given queue
queue:restart Restart queue worker daemons after their current job
queue:retry Retry a failed queue job
queue:table Create a migration for the queue jobs database table
queue:work Start processing jobs on the queue as a daemon
route
route:cache Create a route cache file for faster route registration
route:clear Remove the route cache file
route:list List all registered routes
schedule
schedule:run Run the scheduled commands
session
session:table Create a migration for the session database table
storage
storage:link Create a symbolic link from "public/storage" to "storage/app/public"
vendor
vendor:publish Publish any publishable assets from vendor packages
view
view:clear Clear all compiled view files

產生 controller

接下來要實作

1
2
1. 建立一個簡單的 controller,用來處理 HTTP 請求
2. controller 會載入一個 Hello World 頁面

先執行以下指令

1
2
3
php artisan make:controller Hello             // 沒有預設 code
或是
php artisan make:controller Hello --resource // 有預設 code

執行完之後會產生一個檔案

1
app/Http/Controllers/Hello.php

基本路由

舊版本 Laravel 放在/app/Http/route.php

新版本 Laravel 放在routes/web.php

目前我使用新版本,所以打開web.php後加上

1
2
3
Route::get('/hello',function(){
return 'Hello World!';
});

現在打開http://127.0.0.1/larashop/public/hello

就會出現Hello World字串

現在將剛剛加上的 code 改為

1
Route::get('/hello', 'Hello@index');

再打開/app/Http/Controllers/Hello.php,並改為

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class Hello extends Controller
{
/* 新增下面這段 */
public function index()
{
return 'hello world from controller : )';
}
}

再次打開http://127.0.0.1/larashop/public/hello

就會出現hello world from controller : )

流程如下

1
2
3
-> HTTP 請求 
-> Route 處理(web.php),要送給 controller Hello 裡面的 index func
-> Controller 處理(Hello.php),return 字串

顯示 View 頁面

上面的 controller 會回傳字串,但一般來說要回傳一個網頁頁面

  1. 修改 Route

    1
    2
    3
    Route::get('/hello', 'Hello@index');
    改為
    Route::get('/hello/{name}', 'Hello@show');
  2. 修改 controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Hello extends Controller
{
public function index()
{
return 'hello world from controller : )';
}

// 新增 show function
public function show($name)
{
// 會回傳一個 view
return view('hello',array('name' => $name));
}
}

3.到\resources\views底下建立hello.blade.php,內容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<!DOCTYPE html>
<html>
<head>
<title>Laravel</title>

<link href="//fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css">

<style>
html, body {
height: 100%;
}

body {
margin: 0;
padding: 0;
width: 100%;
display: table;
font-weight: 100;
font-family: 'Lato';
}

.container {
text-align: center;
display: table-cell;
vertical-align: middle;
}

.content {
text-align: center;
display: inline-block;
}

.title {
font-size: 96px;
}
</style>
</head>
<body>
<div class="container">
<div class="content">
<div class="title">Hello {{$name}}, welcome to Laraland! : )</div>
</div>
</div>
</body>
</html>

4.瀏覽http://127.0.0.1/larashop/public/hello/JasonChiuCC,會出現以下畫面表示成功

2017-01-23_144228.png

流程如下

1
2
3
4
-> HTTP 請求 
-> Route 處理(web.php),要送給 controller Hello 裡面的 show func
-> Controller 處理(Hello.php),回傳 hello view
-> 呼叫 view(hello.blade.php)