13
I rarely use MVC for the simple fact that me it seems that each person uses their way, I know that the MVC came before the web, while reading these links I had a feeling that it seems that one person’s understanding is not the same as another for the use of the MVC:
The second link mentions that Controllers are not mandatory and they should only be used in actions
(user actions). If I understood what he said the View is directly responsible for delivering the data to the customer (visually speaking) and it communicates with the Model directly too (without interference from the Controller), whereas the Controller communicates with the Model only when there is user action and is not responsible for delivering anything (it seems that this point is the majority’s agreement).
Popular frameworks that use MVC (try to use?!?)
Three very popular frameworks are Codeigniter, Laravel and cakephp. Everyone works the Routes using the Controller, example with Laravel:
Routes.php:
<?php
Route::resource('', 'DemoController');
Democontroller.php:
<?php
class DemoController extends BaseController
{
public function index()
{
//Chama o views/Demo/index.blade.php
return View::make('Demo.index', compact('data'));
}
...
Demomodel:
index.blade.php:
<!DOCTYPE HTML>
<html>
<body>
<div id='content'>
Output: {{ $data }}!
</div>
</body>
</html>
The equivalent in cakephp:
use Cake\Routing\Router;
Router::connect('/', ['controller' => 'DemoController', 'action' => 'index']);
and creates the file ctp
The equivalent in the codeigneter:
Routes.php:
$route['default_controller'] = 'DemoController';
controller:
class DemoController extends Controller {
private $data;
public function __construct(){
parent::Controller();
}
public function index()
{
$this->data['helloworld'] = 'Hello World';
$this->load->view('Demo', $this->data);
}
}
Doubt
If me understood, although the Model data does not pass through the Controller yet the Controller is responsible for calling the Model and the View (at least in the frameworks cited), or the Controller calls the View and this calls the Model, anyway the controller is the trigger (if I understand correctly).
In other words, whether you want it on the Web or not, the Controller always goes if make necessary, not only for reasons of routes, but also for the simple reason of anything on a web page present the CRUD meaning (Create, Read, Update, Delete), when opening the home page of a website we are making a READ, no?
So my question is: Frameworks when using controllers are wrong or any HTTP request is considered a action
and this would technically be user action (which would require routes to be Controllers)?
Or really nobody uses MVC strictly?
Related: What is MVP and MVVM?
– ptkato
Personal opinion, I think that controller in backend is something that tends to cease to exist, since having model that defines rules of access and representation, you only need an interface to deliver in some protocol like REST, Websocket, or non-standard format (which is the only one that benefits from a controller, usually replaceable by a callback on the router). MVC is an interesting separation of responsibilities, but that made sense when we did not use SPA, nowadays adopt it tends to add unnecessary complexity in the backend. Already on the front is another story :)
– Gabriel Gartz
@Gabrielgartz It seems very logical your comment, I searched SPA, but did not find, would SOA (Service Oriented Architecture)?
– Guilherme Nascimento
SPA is Single Page Application (where your website behaves like an application, loading date and modules as efficiently as possible and only crossing data to populate the interface and operations that can be done on the client instead of the server)is usually strongly related to a SOA or Saas. But these are another layer of architecture of your application, which can be developed by combining with MVC or other architecture patterns to get the desired results.
– Gabriel Gartz
@Gabrielgartz I analyzed an unpopular framework and its documentation, the mini2, in version 1 it used controllers, but in version 2 it uses only Views and Models combined the functions Anonimas for routes.
– Guilherme Nascimento
@Guilhermenascimento very cool this framework, I did not know, I only knew the SLIM for PHP, I think it’s cool that PHP Developers are copying the functional style of Node (and their libs), I find simpler and less verbose than the frameworks that try to copy Java, And to this day PHP has no typed property and using magic methods for this is a terrible solution. But let’s not get away too much of the subject, if you want to have a chat about these issues out of the comments adds me on FB. :)
– Gabriel Gartz