gpt4 book ai didi

php - Laravel - 可重用的资源 Controller

转载 作者:可可西里 更新时间:2023-10-31 22:13:05 25 4
gpt4 key购买 nike

我正在修改我在项目中使用的 CMS,最近我决定为默认操作创建一个 Controller BaseController,所有其他 Controller 都将扩展此 Controller BaseController.

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

class BaseController extends Controller
{
protected $viewFolder = 'admin';

protected $title;

protected $model;

protected $key = 'id';

protected $files = [];

public function __construct()
{
$this->setVariable('title', $this->title);
}

public function index()
{
$items = $this->model::paginate();

$this->setVariable('items', $items);

return $this->viewRender('index');
}

public function create()
{
return $this->viewRender('create');
}

public function store(ExampleStoreRequestFROMEXAMPLECONTROLLER $request)
{
$item = $this->model::create($request->all());

return redirect()->route($this->viewFolder.'.'.$this->viewType.'.show', $item[$this->key]);
}

public function show($id)
{
$item = $this->model::where($this->key, $id)->firstOrFail();

$this->setVariable('item', $item);

return $this->viewRender('show');
}

public function edit($id)
{
$item = $this->model::where($this->key, $id)->firstOrFail();

$this->setVariable('item', $item);

return $this->viewRender('edit');
}

public function update(ExampleUpdateRequestFROMEXAMPLECONTROLLER $request, $id)
{
$item = $this->model::where($this->key, $id)->firstOrFail();

$item->update($request->except(['_token', '_method']));

return redirect()->route($this->viewFolder.'.'.$this->viewType.'.show', $item[$this->key]);
}

public function status(ExampleStatusRequestFROMEXAMPLECONTROLLER $request, $id)
{
$this->model::where($this->key, $id)->update($request->except('_method'));

return response()->json([
'message' => 'O status foi alterado com sucesso'
]);
}

public function destroy($id)
{
$this->model::where($this->key, $id)->delete();

return redirect()->route($this->viewFolder.'.'.$this->viewType.'.index');
}
}

问题是:我使用 UserController 的表单请求编写了 BaseController,但我不知道如何让这些请求保持动态以便我可以从其他 Controller 。

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\ExampleStatusRequest;
use App\Http\Requests\ExampleStoreRequest;
use App\Http\Requests\ExampleUpdateRequest;

class ExampleController extends BaseController
{
protected $viewType = 'users';

protected $model = 'App\Example';

public function index()
{
$this->setVariable('title', 'Usuários');
return parent::index();
}

public function create()
{
$this->setVariable('title', 'Cadastrar usuário');
return parent::create();
}

public function store(ExampleStoreRequest $request)
{
return parent::store($request);
}

public function edit($id)
{
$this->setVariable('title', 'Editar usuário');
return parent::edit($id);
}

public function update(ExampleUpdateRequest $request, $id)
{
return parent::update($request, $id);
}

public function status(ExampleStatusRequest $request, $id)
{
return parent::status($request, $id);
}
}

这是我的默认 Controller :

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

protected $viewFolder = '';

protected $viewType = '';

protected $viewVariables = [];

protected function setVariable($key, $value)
{
$this->viewVariables[$key] = $value;
}

protected function viewRender($view)
{
return view($this->viewFolder.'.pages.'.$this->viewType.'.'.$view, $this->viewVariables);
}
}

知道如何解决这个问题吗?

最佳答案

在评论之后我可以更好地理解。我在这里遇到了同样的问题,这是我的解决方法

class BaseController extends Controller
{

protected function _store($request)
{
....
}
...
}

class MyController extends BaseController
{
....
public function store(MyRequest $request)
{
//do something
return parent::_store($request);
}
}

关于php - Laravel - 可重用的资源 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50182156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com