[Laravel] LAMP+Laravel 學習筆記-route/controller/view

繼上篇基本安裝 (Laravel )
本篇繼續Laravel 實作
1.routing
2.view
3.controller
利用php artisan make:controller 產生controller

 

  • 重要指令: php artisan make:controller

                view('hello',array('name'=>$name));
                Route:get('hello','hello@index')
                blade:{{name}}

1.Laravel的artisan 指令 #方便快速產生模板

>php artisan --list #列出所有指令

>php artisan list
Laravel Framework 5.4.21

Usage:
  command [options] [arguments]
...
...
 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
..
..
以下略....

 

2.php artisan make:controller Hello --resource   #包含resource CRUD

php artsian make:controller Hellor                    #no CRUD ,only plain
自動產生Controller : \app\HttpControllers\hello.php

3.hello.php

以下為Controller:hello
並可執行action: index

<?php

namespace App\Http\Controllers;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

class hello extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {			  	
			  	return '<h1>hello this is hello controller index action</h1>';
    }
    

4.1 新增路由 Routing : hello
 修改: /routes/web.php

Route::get('hello', 'hello@index');

語法說明:Route::get('Route名稱','Controller稱@Action名稱');

4.2 測試: http://192.168.1.2/blog/public/hello
大功告成:     show "this is hello controller index action";


4.3 routing 及 參數{name}
修改:/routes/web.php
新增路由(Routing ): hello/{name}  --> 此路由呼叫 controller : hello 的action: show  ,並會帶入參數:$name

Route::get('hello/{name}', 'hello@show');  //call action:show

4.4 修改 controller : /app/Http/Controllers/hello.php
傳入變數$name,並回傳view:hello
預設載入 /resources/views/hello.blade.php

public function show($name)
    {
        return view('hello',array('name' => $name));
    }

4.3 修改: view in /resources/views/hello.blade.php  #使用blade 模板
name 變數: {{$name}} 

   <body>
        <div class="container">
            <div class="content">
                <div class="title">Hello {{$name}}, welcome to Laraland! : )</div>
            </div>
        </div>
    </body>

4.5 測試: http://192.168.1.2/blog/public/hello/mike

              Hello mike, welcome to Laraland! : )

 

  • 到這邊基本的routing,controller,view的應用已經完成~~



參考
1.https://www.laravel.tw/docs/5.3