Home > PHP Framework > Laravel > body text

Controllers in Laravel

灭绝师太
Release: 2021-12-06 15:21:09
Original
2168 people have browsed it

Controllers in Laravel

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

1. Simply create a controller

//使用php artisan 命令,创建好后会自动加载命名空间,自动基础控制器基类
php artisan make:controller [控制器路径/]控制器名称
Copy after login

2. Classification of controllers

1. Basic controller

//app/http/controller/定义UserController
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\User;

class UserController extends Controller
{
    /**
     * 显示指定用户的简介
     *
     * @param  int  $id
     * @return \Illuminate\View\View
     */
    public function show($id)
    {
        .......
    }
}
Copy after login

Used in routing:

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

2. Single behavior controller

//使用命令行方式创建单行为控制器
php artisan make:controller	GetName --invokable
Copy after login
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\User;

class GetName extends Controller
{
    /**
     * 显示指定用户的简介
     *
     * @param  int  $id
     * @return \Illuminate\View\View
     */
    public function __invoke($id)
    {
        .........
    }
}
Copy after login

Used in routing:

Route::get('user/{id}', GetName::class)
Copy after login

3. Resource controller

//使用命令行方式创建资源控制器
php artisan make:controller UserController --reosurce
Copy after login
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Good;
use App\Transformers\GoodTransformer;
use Illuminate\Http\Request;

class GoodController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

    }

    /**
     * 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)
    {

    }

    /**
     * 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)
    {
        //
    }
}
Copy after login

Used in routing:

//第二参数为选填,例如当不需要destory()方法时,即可填入
Route::resource(&#39;goods&#39;,\App\Http\Controllers\Admin\GoodController::class,[
      &#39;except&#39; => &#39;destroy&#39;
]);
Route::resource(&#39;goods&#39;,\App\Http\Controllers\Admin\GoodController::class,[
      &#39;only&#39;  => [&#39;index&#39;,&#39;show&#39;,&#39;store&#39;,&#39;update&#39;]
]);
Copy after login

Related videos Tutorial recommendation: Laravel Video Tutorial

The above is the detailed content of Controllers in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template