Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create Controller in Laravel

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to create controllers in Laravel. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

To replace all request processing logic defined as closures in the routing file, if you want to use control classes to organize these behaviors, the controller can organize the relevant request processing logic into a separate class, and the controller is stored in the app/Http/Controllers directory.

First, simply create a controller

/ / use the php artisan command to automatically load the namespace after creation, and automatically base controller base class php artisan make:controller [controller path /] controller name

Second, the classification of controllers

1. Basic controller

/ / app/http/controller/ definition UserControllernamespace App\ Http\ Controllers;use App\ Http\ Controllers\ Controller;use App\ Models {/ * display the profile of the specified user * * @ param int $id * @ return\ Illuminate\ View\ View * / public function show ($id) {. }}

Use in routing:

Route::get ('user/ {id}', [UserController::class, 'show'])

two。 Single behavior controller

/ / create a single behavior controller php artisan make:controller GetName-- invokablenamespace App\ Http\ Controllers;use App\ Http\ Controllers\ Controller;use App\ Models\ User;class GetName extends Controller {/ * display the profile of the specified user * * @ param int $id * @ return\ Illuminate\ View\ View * / public function _ _ invoke ($id) {. }}

Use in routing:

Route::get ('user/ {id}', GetName::class) 3. Resource controller

/ / create a resource controller php artisan make:controller UserController-- reosurce using command line mode

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report