mardi 12 mars 2019

Adcstract controller

I have a standard controller, whose logic the same in other controllers. These are admin panel controllers.

<?php

namespace App\Http\Controllers;

use App\Http\Requests\PageRequest;
use App\Page;
use App\Repositories\PageRepository;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class PageController extends Controller
{
    private const REDIRECT_INDEX = 'PageController@index';
    protected $model;

    public function __construct()
    {
        $this->model = new PageRepository(new Page());
    }

    public function index(): View
    {
        $pages = $this->model->all();

        return view('pages.index', compact('pages'));
    }

    public function create(): View
    {
        return view('pages.create');
    }

    public function store(PageRequest $request): RedirectResponse
    {
        (new Page($request->all()))->save();

        return redirect()->action(self::REDIRECT_INDEX)->with('status', 'Created');
    }

    public function show(Page $page): View
    {
        return view('pages.show', compact('page'));
    }

    public function edit(Page $page): View
    {
        return view('pages.edit', compact('page'));
    }

    public function update(PageRequest $request, Page $page)
    {
        $page->fill($request->all())->save();

        return redirect()->action(self::REDIRECT_INDEX)->with('status', 'Updated');
    }

    public function destroy(Page $page): RedirectResponse
    {
        $page->delete();

        return redirect()->action(self::REDIRECT_INDEX)->with('status', 'Deleted');
    }
}

In other controllers different only

  • const REDIRECT_INDEX
  • $model
  • vies - 'pages.index', 'pages.create' and etc.
  • PageRequest $request - request with validation
  • Page $page - auto finded row from db by slug

So I have PageController, NewsController, TabController, TypeController with the same logic. How can I abstract?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2JmULur
via IFTTT

Aucun commentaire:

Enregistrer un commentaire