Pendo's avatar
Level 10

Best practice: crud pages on both front and back-end

Hi all,

as I'm getting deeping into a project, there are a few things that I'm seeking the best practice for. Most of these things I'll figure out myself, but I'd like to know how you guys tackle this particular problem.

Problem:

I'm working on an Agenda module for a website, in the back-end the administrator kan view, edit, create or delete all records in the database. On the front-end however, users can create items and edit their own. How would I structure the code so that both codes can use functions that are no different at all. Right now I have 2 controllers:

  • App\AgendaController.php
  • App\Admin\AgendaController.php

As far as I can see right now, the create and edit methods are exactly the same, also the methods to upload or remove an image that is connected to an item won't change at all. The only thing different will be the views they're calling.

I was thinking of one extending the other, but the fact that views aren't shared makes that not work. Should I accept the fact that I'm having duplicate code in this case? Or is there anything I haven't thought of?

0 likes
3 replies
jlrdw's avatar

How about using the Gate Facade, that way your views still have edit / delete buttons if desired, but you can control who can edit what.

1 like
Pendo's avatar
Level 10

Doh, I feel a bit stupid right now.. I can even use a shared base controller using the Gate facade. Thanks.

1 like
jlrdw's avatar

Yea, laravel is pretty big, and sometimes things escape us. I wish you the best on your project.

2 likes

Please or to participate in this conversation.