MVC - Doubt about controller and view PHP

Asked

Viewed 1,588 times

2

I’m studying MVC a few days ago and I came across a question, I need a view to register, but this view would not do anything just send the data my doubt is that I need a control just to set up this view and another to register it?

  • On the web, the understanding of MVC is a little different from software that works with signals (such as desktop applications). I mean, any request made via HTTP needs a Controller.

  • I understood, so I must create a control to assemble the form and another to receive this data.

  • Yes you’re right André, in case a controller can have several actions, then you can have a controller that accesses an action, then you can have a controller only, but with several actions - a doubt which framework you are using?

  • I am not using any at the moment it is for educational purposes only, as I prefer to understand first how this type of application works before using frameworks.

  • "mount a form"... this is more like helper.. search for "html helper". It’s not necessarily a library or controller.

  • @Danielomine a good tip, only one detail I’m sure you know and it’s only for other users not to confuse is that the "html helper" (or equivalent) should be used within the View.

Show 1 more comment

1 answer

2


HTTP applications change somewhat compared to desktop applications, for example C++ software that uses MVC has signal support, so even a button can be a "controller".

Already in HTTP frameworks until the request the index page must be a Controller/action, examples:

Laravel:

Laravel, like other newer frameworks, uses routes to access controllers, for example:

<?php
Route::get('/', 'HomeController@getIndex');

The Controller must be something like:

<?php
class HomeController extends UserController
{
    public function getIndex()
    {
        return View::make('formulario');//Mostra o seu view
    }
}

Note that in the example the View does not receive data from the Model, but probably the Model form will access another action within the HomeController (or another Controller) that will use one Model.

cakephp:

In cakephp you should edit the file app/config/routes.php, should look something like:

Router::connect('/', array(
                       'controller' => 'MeuController',
                       'action' => 'index'
                     ));

This action index do not necessarily need to access a Model, assuming it contains your form

Assuming the form points to a page like http://example/usuario/cadastrar then you will have to add a route like this:

Router::connect('/usuario/cadastrar', array(
                       'controller' => 'MeuController',
                       'action' => 'cadastrar'
                     ));

Or another Controller

Router::connect('/usuario/cadastrar', array(
                       'controller' => 'UserController',
                       'action' => 'cadastrar'
                     ));
  • 1

    Oh yes that’s what I was trying to understand, as I’m not using any framework, I was in doubt about actions that do not receive additional data, thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.