2

I'm developing a PHP MVC REST API which will have OAuth for authenticating if the call can be performed.

So my plan is to develop it for an Angular 2/4 app which will handle the API calls.

The idea was to create a structure where my domain for example test.com will have a subdomain api.test.com.And from it the API calls will be requested.

The thing that i would like to confirm before i start building this is my MVC backend structure. enter image description here

Ref.Pic

  • Model will have all the logic
  • View will be the api.test.com/api/call presentation.Outputing the JSON in my case.
  • Controller will accept input exmp:the api.test.com/api/call call the logic in the Model if the call is correct.Than the call will react on the view where presenting the JSON.It would hold the OAuth also....

I'm trying to understand the whole concept of this, since i have tried several times to build it and every time i end up in a mess of code.I'd like to see if my understanding of the project above makes sense.

Thank you

Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
DaAmidza
  • 336
  • 2
  • 7
  • 25
  • https://stackoverflow.com/questions/5863870/how-should-a-model-be-structured-in-mvc read tereškos answer.As I undarstood it holds everything ,than the controller access the logic.So the controller is the validator and choses which logic to pick..Correct me if im wrong – DaAmidza May 24 '17 at 18:28
  • @ tereško help :D – DaAmidza May 24 '17 at 18:31
  • 1
    Makes sense,you should check out this project https://github.com/knpuniversity/oauth –  May 24 '17 at 20:59
  • Thank you I'l check it out :D – DaAmidza May 24 '17 at 21:00

2 Answers2

5

There are multiple issues, that you will encounter as you try to apply SoC principle in your codebase.

And, what to do with communication between views and controllers, is one of the more visible ones. That's because, if you mess it up, there are no abstractions to hide it all behind.

The two major approaches are Supervising Controller and Autonomous View (which has no article, but you can deduce some of it from this and this).

Supervising controller

This approach is best suited for smaller applications (well .. for large values of "small", since really small projects don't really need the architectural bloat of MVC). It essentially looks like this:

/* in a controller */
public function verifyEmail(Request $request)
{

    $identity = $this->search->findEmailIdentityByToken(
        $request->get('token'),
        Identity::ACTION_VERIFY
    );
    $this->registration->verifyEmailIdentity($identity);

    $body = [
        'status' => 'ok',
    ];

    return new JsonResponse($body);
}

Essentially, what you have there is a controller, that interacts with the business model and (when necessary) populates the view with data. Then it causes the view to be rendered and returns the response.

In my own experience, I have found this to be the best approach to use, when writing backend applications, that are expected to only be manipulated via REST-like API.

As you see in the example, the "view" in this case is extremely trivial. It is basically array, that you render as JSON (which the provided code would actually do - it's copied from a real project).

Note:
If your intention is to fully implement REST as it was proposed (not just the REST-like resource endpoints) and/or have functionality of resource expansion, then this approach might be wrong and even harmful.

Autonomous View

When your presentation logic becomes complicated or have to provide different UIs for the same application with the same functionality (like having single app with both website and REST API, with both xml an json interface), then using Supervising Controller becomes a ball and chain around your neck. Your controllers start to grow uncontrollably and your project can be described as "legacy codebase", before it even reaches production.

And that's where you use this approach.

You use views and controllers as completely separate classes and you interact of their instances at the same layer (for example: bootstrap stage). It ends up looking something like this:

/* in /src/application.bootstrap.php */
$command = $request->getMethod() . $parameters['action'];
$resource = $parameters['resource'];

$controller = $container->get("controllers.$resource");
if (method_exists($controller, $command)) {
    $controller->{$command}($request);
}

$view = $container->get("views.$resource");
if (method_exists($view, $command)) {
    $response = $view->{$command}($request);
    $response->send();
}

Again, example from a different live project, which also uses a DI container. In this case there is only one UI (hence, no "type" prefix, when making a view instance), which mean that the code can take advantage of 1:1 relation between controllers views (it would be 1:n, if you need multiple UIs).

The controller in this case basically only "writes" to the model layer. And the view (which also has access to services from model layer) only performs "reads" and extracts only the information, that it requires for populating templates and rendering them.

And, if your presentation logic grows further, it is a good idea to start adding presentation objects, that will contain the repeating parts of the presentation logic (e.g. deciding, which menu item in the sidebar has to be expanded and which submenu item has to be highlighted), that are common for multiple views.

If your backend application only deals with API, then this approach might be too complex, unless you are doing one of the things mentioned in the "note" part.

... maybe this helps a bit

Jo.
  • 778
  • 1
  • 12
  • 17
tereško
  • 58,060
  • 25
  • 98
  • 150
-2

Yes, That makes sense. I suggest using the Laravel framework instead. Or the OctoberCMS even better.

George Peter
  • 69
  • 1
  • 1
  • 6
  • No i don't like using framework like Laravel.Sounds odd but my way of thinking.I try to write a lot of my code,and some frameworks or libraries which i use.I undarstand the whole problem better.Larvel as it self doesn't use MVC in a strice way...but its another topic to talk about :D – DaAmidza May 24 '17 at 18:37
  • Come on, The Laravel or the OctoberCMS have an excellent environment for you, even you could install OAuth as a plugin. So that you could focus your logic. Similar that you use Model instead of plain SQL. – George Peter May 24 '17 at 19:40
  • 3
    I don't use simple stuff :D Please there is no point of convincing me.If you know answer the quetion with references, please stop convincing me that i should use Larvel or OctoberCMS. – DaAmidza May 24 '17 at 20:51
  • It's none of my business whether you use or not. I was talking the framework has many features that you may probably need to establish. – George Peter May 24 '17 at 21:15
  • 1
    Laravel is typically **not** made to serve as a backend service that only returns resources.... – Félix Adriyel Gagnon-Grenier May 25 '17 at 20:54