[Laravel] Laravel的基本使用 laravel執行個體教程 laravel學院 laravel架構下載

來源:互聯網
上載者:User
[Laravel] Laravel的基本HTTP路由

使用Laravel的基本路由,實現get請求響應,找到檔案app/Http/routes.php

調用Route的靜態方法get(),實現get響應,參數:string類型的路徑,匿名函數function(){}

匿名函數內部,返回string資料

實現post,put,delete的請求,同上

實現get傳遞參數的路由,調用Route的靜態方法get(),參數:路徑,匿名函數

路徑,大括弧包裹參數名,不含$,例如:’/user/{id}’

匿名函數,接收參數,例如:function($id){}

[Laravel] Laraval的基本控制器

在app/Http/Controllers目錄下,建立一個Index/IndexController.php

定義命名空間,namespace App\Http\Controllers\Index

引入Controller基本控制器,use App\Http\Controllers\Controller

定義IndexController繼承Controller

實現方法index,返回資料

定義路由指定控制器的行為,例如:Route::get("/index","Index\IndexController@index");,

注意命名空間部分,建立的控制器是在根命名空間下面,指定的時候添加自己新加的命名空間

[Laravel] Laravel的基本視圖

在目錄resources/views/下面,建立index/index.php

在控制器中使用函數view()來調用模板,參數:檔案路徑(.分隔目錄),資料

路由:routes.php

php/*|--------------------------------------------------------------------------| Routes File|--------------------------------------------------------------------------|| Here is where you will register all of the routes in an application.| It's a breeze. Simply tell Laravel the URIs it should respond to| and give it the controller to call when that URI is requested.|*//*測試get post*/Route::get('/', function () {    $url=url("index");    return "Hello World".$url;    //return view('welcome');});Route::post("/post",function(){    return "測試post";});/*傳遞參數*/Route::get("/user/{id}",function($id){    return "使用者".$id;});/*使用控制器*/Route::get("/index","Index\IndexController@index");/*|--------------------------------------------------------------------------| Application Routes|--------------------------------------------------------------------------|| This route group applies the "web" middleware group to every route| it contains. The "web" middleware group is defined in your HTTP| kernel and includes session state, CSRF protection, and more.|*/Route::group(['middleware' => ['web']], function () {    //});

控制器:IndexController.php

phpnamespace App\Http\Controllers\Index;use App\Http\Controllers\Controller;class IndexController extends Controller{    public function index(){        $data=array();        $data['title']="Index控制器";        return view("index.index",$data);    }}

模板:index.php

<body><div class="container"><div class="content"><div class="title">php echo $title;?>div>div>div>body>

以上就介紹了[Laravel] Laravel的基本使用,包括了laravel方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.