doudiejian5827 2018-04-10 23:52
浏览 30
已采纳

laravel one controller,用于具有相同模式的不同模型

Hello I am developing an application with a Laravel api server and a C# Winforms frontend. The application has many database models, but most of them consist only in an id field and a name field. No more operations than storing and retrieving that data is needed. The data can't be combined into one table (technically is possible, but it would be a mess) because is totally different data,from postal codes and associated city to families of products.

So I thought, as in C# I could make a generic form, if there is possible to make this in Laravel as it will speed up my work too much.

Edit: Here I attach one example controller I want to make it Generic.

<?php

namespace App\Http\Controllers;

use App\Http\Requests\LocalidadRequest;
use App\Localidad;
use Illuminate\Http\Request;

class LocalidadsController extends Controller
{
    public function index()
    {
        return response()->json(array("data" => Localidad::all(), "error" => 0), 200);
    }

    public function show(Localidad $localidad)
    {
        return $localidad;
    }

    public function store(LocalidadRequest $request)
    {

        $localidad = Localidad::create($request->all());

        return response()->json($localidad, 201);
    }

    public function update(LocalidadRequest $request, Localidad $localidad)
    {
        $localidad->update($request->all());
        return response()->json($localidad, 201);
    }

    public function delete(Localidad $localidad)
    {
        $localidad->delete();
        return response()->json(null, 204);
    }
}

I would like to generalize this controller like this (this isn't real code):

public function index()
{
    return response()->json(array("data" => ??::all(), "error" => 0), 200);
}

public function show(Model $routeModel)
{
    return $routeModel;
}

public function store(CommonRequest $request)
{

    $routeModel = ??::create($request->all());

    return response()->json($routeModel, 201);
}

public function update(CommonRequest $request, Model $routeModel)
{
    $routeModel->update($request->all());
    return response()->json($localidad, 201);
}

public function delete(Model $routeModel)
{
    $routeModel->delete();
    return response()->json(null, 204);
}

Thanks

展开全部

  • 写回答

1条回答 默认 最新

  • douhao2153 2018-04-11 01:55
    关注

    It is possible and can structure the route and controller as follows:

    Define a generic controller:

    abstract class CommonController extends Controller
    {
        abstract protected function getModel();
    
        public function index()
        {
            return response()->json(array("data" => ($this->getModel())::all(), "error" => 0), 200);
        }
    
        ....
    }
    

    For example, if you have city model, then you can define a city controller and inherit the generic controller:

    class CityController extends CommonController
    {
        protected function getModel()
        {
            return App\City::class;
        }
    }
    

    For route, register it to corresponding controller:

    Route::get('cities', 'CityController@index');
    ....
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部