Propaganistas
Propaganistas

Reputation: 1792

Laravel user forms and admin forms: how to tackle?

Just a question to poll how you guys would tackle this in Laravel: I have a user preferences page defined in UserController.php which creates the view at user/preferences.blade.php. Administrators should of course be able to edit the user's preferences and have some extra administrative fields shown to be changed. Furthermore I'd like to collect all admin functionality concerning users in a separate controller called AdminUserController.php

I'm thinking of some possibilities to achieve this functionality:

I'm curious about how you would tackle this! Thanks.

Upvotes: 1

Views: 526

Answers (1)

Sergey Telshevsky
Sergey Telshevsky

Reputation: 12197

This is mostly a question of preference, but I really suggest you to completely separate all that is possible here. Administration is a process that is very sensitive and not in any way should it be possible, that a normal user will be able to see it under any circumstance.

I believe that we all are humans and we make mistakes more or less often, that's why we need to make sure that our mistakes in not assigning right value to the right variable or making a type of = instead of == not to ruin business logic.

I think you should make a separate view and a separate controller for user self management and administration and never tie them up. If you want to keep your code DRY as much as possible, you may extend your user controller and model from your admin controller and model, but not the other way around. That's just my 2 cents, it all depends on what type of application you are working on and what the stakes are.

<?php
class AdminController extends UserController
{
    public function __construct(AdminModel $model)
    {
        // Use dependency injection
        $this->model = $model;
    }

    // In the original UserController class:
    public function postPreferences($user) {
        $this->model->edit($user, Input::all());
        // you may do it this way so your user only saves user data and
        // you admin model saves all the data including administrative
    }

    // ...
}

Upvotes: 1

Related Questions