laravel框架中控制器的創(chuàng)建和使用方法分析
本文實(shí)例講述了laravel框架中控制器的創(chuàng)建和使用方法。分享給大家供大家參考,具體如下:
laravel中我們可以使用 artisan 命令來(lái)幫助我們創(chuàng)建控制器文件。
php artisan make:controller TestController
TestController 控制器名我們可以任意指定。文件默認(rèn)會(huì)創(chuàng)建在 app\Http\Controllers 目錄下。
打開(kāi)控制器文件,我們就可以添加自已的方法了。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class TestController extends Controller { public function test() { echo 'test...'; } }
在路由文件 routes/web.php 中配置路由就可以訪問(wèn)了。
Route::get('/test', 'TestController@test');
如何獲取用戶(hù)的輸入,一般推薦通過(guò)依賴(lài)注入的方式來(lái)獲取。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class TestController extends Controller { public function test(Request $request) { //獲取所有請(qǐng)求數(shù)據(jù) $data = $request->all(); //獲取指定請(qǐng)求數(shù)據(jù) $id = $request->input('id'); } }
laravel中為我們編寫(xiě) restful 風(fēng)格的代碼,提供了簡(jiǎn)單方式,只需在創(chuàng)建控制器命令后面加上 --resource 選項(xiàng)。
php artisan make:controller OrderController --resource
laravel幫我們創(chuàng)建指定的方法,各自表示不同的意義和作用。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class OrderController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { // } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { // } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { // } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { // } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { // } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { // } }
具體方法的作用如下所示:
HTTP 方法 | URI | 控制器方法 | 路由名稱(chēng) | 作用描述 |
GET | /order | index | order.index | 顯示所有訂單列表 |
GET | /order/create | create | order.create | 顯示創(chuàng)建訂單頁(yè)面 |
POST | /order | store | order.store | 接收提交數(shù)據(jù),創(chuàng)建訂單 |
GET | /order/{id} | show | order.show | 顯示單個(gè)訂單信息 |
GET | /order/{id}/edit | edit | order.edit | 顯示修改訂單頁(yè)面 |
PUT/PATCH | /order/{id} | update | order.update | 接收提交數(shù)據(jù),修改訂單 |
DELETE | /order/{id} | destroy | order.destroy | 刪除訂單 |
最后我們通過(guò) Route::resource() 來(lái)綁定上面的所有路由。
Route::resource('order', 'OrderController');
我們也可以通過(guò)命令查看,綁定的路由列表。
php artisan route:list
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Laravel框架入門(mén)與進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
- Laravel 5框架學(xué)習(xí)之模型、控制器、視圖基礎(chǔ)流程
- Laravel5.1數(shù)據(jù)庫(kù)連接、創(chuàng)建數(shù)據(jù)庫(kù)、創(chuàng)建model及創(chuàng)建控制器的方法
- Laravel 5框架學(xué)習(xí)之路由、控制器和視圖簡(jiǎn)介
- Laravel框架路由和控制器的綁定操作方法
- Laravel重定向,a鏈接跳轉(zhuǎn),控制器跳轉(zhuǎn)示例
- laravel通過(guò)a標(biāo)簽從視圖向控制器實(shí)現(xiàn)傳值
- Laravel框架控制器的middleware中間件用法分析
- Laravel框架控制器的request與response用法示例
- laravel框架模型、視圖與控制器簡(jiǎn)單操作示例
- Laravel框架控制器,視圖及模型操作圖文詳解
- Laravel框架中的路由和控制器操作實(shí)例分析
- Laravel 框架控制器 Controller原理與用法實(shí)例分析
相關(guān)文章
TP3.2批量上傳文件或圖片 同名沖突問(wèn)題的解決方法
這篇文章主要為大家詳細(xì)介紹了TP3.2批量上傳文件或圖片,同名沖突問(wèn)題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Laravel框架中composer自動(dòng)加載的實(shí)現(xiàn)分析
Laravel作為在國(guó)內(nèi)國(guó)外都頗為流行的PHP框架,風(fēng)格優(yōu)雅,其擁有自己的一些特點(diǎn)。下面這篇文章主要給大家介紹了關(guān)于Laravel框架中composer自動(dòng)加載實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-12-12Zend Framework教程之模型Model基本規(guī)則和使用方法
這篇文章主要介紹了Zend Framework教程之模型Model基本規(guī)則和使用方法,結(jié)合實(shí)例形式詳細(xì)分析了Zend Framework中模型的原理與具體使用技巧,需要的朋友可以參考下2016-03-03Thinkphp5.0 框架Model模型簡(jiǎn)單用法分析
這篇文章主要介紹了Thinkphp5.0 框架Model模型簡(jiǎn)單用法,結(jié)合實(shí)例形式分析了thinkPHP5.0框架模型Model常用方法的簡(jiǎn)單使用技巧,需要的朋友可以參考下2019-10-10在 Laravel 中 “規(guī)范” 的開(kāi)發(fā)短信驗(yàn)證碼發(fā)送功能
Laravel是一套簡(jiǎn)潔、優(yōu)雅的PHP Web開(kāi)發(fā)框架(PHP Web Framework)。接下來(lái)通過(guò)本文給大家分享在 Laravel 中 “規(guī)范” 的開(kāi)發(fā)短信驗(yàn)證碼發(fā)送功能,需要的朋友參考下吧2017-10-10