MVC - Intermediate php file in the View and Controller interface

Asked

Viewed 379 times

1

Hey there, guys. I was reflecting on the existence of a relationship step of the structure of my project.

The organization of my project is as follows:

inserir a descrição da imagem aqui

[1 ] When accessing the page the user can access both the normal address (ex. curso.php) or with parameters (ex. curso.php?curso=sistema_informacao). In the case of access by normal address, it is usually accessed by the button you have in the file index.php. From the action of the user the file curso.php makes a check if any parameter has been passed, where it is normally passed via ajax in step [3].

[2] As soon as the file curso.php finishes checking parameters, it calls the file view_curso.php. The address will remain as .../curso.php

[3] Through user actions javascript makes POST via ajax to the archive curso.php and one of the parameters that is sent is called opcao, thus the curso.php knows which controller function to call, step [4].

function pesquisar(){
    var curso = documento.getElementById("id_curso").value;
    $.ajax({
        type: "POST",
        url: "curso.php",
        data: {
            "curso": curso,
            'opcao': 'pesquisar_disciplinas'
        }
    });
}

[4] The controller checks the action and if the parameters are correct it calls the class that will perform that action.

[5] In this example, the action being a select, then the controller calls the model_curso.php.

[6] The model_curso.php returns the result to the controller.

[7] The controller checks, and in this example it calls the file view_painel_curso.php with the results of model_curso.php.

The question I have is whether there really is a need to exist the file curso.php.

Basically, the file curso.php is a view and controller intermediary, and has the function to filter what is being requested by the view and pass to the controller that then dictates who will perform.

Is there any variation of MVC that uses this architecture that I’m using or can we say that it’s still MVC? Do you use any similar structure?

php course.

<?php

# Importar bibliotecas e classes
require_once "../require.php";

# Importar controlador
require_once "./controllers.php";

// mostrar exceções de banco de dados
$APP->getDBLink()->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); 

# exigir que usuário esteja logado
$APP->requireLoggedUser(true);

# Inicializar o objeto controlador
$Ctrl = new \curso\Controller($APP);

# método da requisição
$reqmethod = $_SERVER['REQUEST_METHOD'];

if( $reqmethod == "POST" ) {
    $opcao          = filter_input(INPUT_POST, "opcao");

    // salvar disciplina
    if ($opcao == "salvar_curso"){
        $Ctrl->salvarCurso(['POST' => $_POST]);
   // pesquisar curso
    } elseif($opcao == "pesquisar_disciplinas"){
        $Ctrl->getDisciplinas($_POST);

    }
} else {

    $curso = filter_input(INPUT_GET, "curso");
    $disciplina = filter_input(INPUT_GET, "disciplina");

    if(isset($curso)) {

        $APP->requireLoggedUser(true);

        # Define qual parte do controlador vai usar
        $content = function() use($Ctrl) {
            // 
            $Ctrl->pesquisar(['GET' => $_GET ]);
        };
    } else {
        $content = function() use($Ctrl){
            $Ctrl->indexAction('view_curso');
        };
    }

    # qual template HTML
    $qual_template = 'basico2';

    # título da página
    $title = 'Cursos Faculdade';

    $options['css']['files'][] = '<link href="static/estilos.css" rel="stylesheet" type="text/css">';
    $options['js_files'][] = '<script src="./static/assets/moment/moment.js"></script>';
    $options['js_files'][] = '<script src="./static/assets/moment/moment-with-locales.js"></script>';
    $options['js_files'][] = '<script type="text/javascript" src="static/curso.js"></script>';

    $options['favicon'] = '<link  id="favicon" rel="shortcut icon" href="/static/imgs/icone.png" type="text/css" />';

    # chamar o template 
    $APP->printTemplate( $qual_template, $title, $content, $options );
}
?>

1 answer

2


"- Is there any variation of the MCV that uses this architecture that I’m using or can we say that it’s still the MCV? Do you use any similar structure?"

I’ll assume your real problem is about structuring your code in order to follow the pattern MVC. 'Cause questions like: "- Do you use any similar structure?" end up not being a real problem, as defined in community aid centre.

Let’s say you’re running away from the pattern theory MVC the moment you intermedia the actions of each of its sectors (Model, View and Controller).

See that your file curso.php is mixing the service of various sectors of the MVC: Starting the application, calling the controller, calling the model and rendering. See:

# Importar bibliotecas e classes
require_once "../require.php";

# Importar controlador
require_once "./controllers.php";

// ...

# Inicializar o objeto controlador
$Ctrl = new \curso\Controller($APP);

# método da requisição
$reqmethod = $_SERVER['REQUEST_METHOD'];

// ...

# chamar o template 
$APP->printTemplate( $qual_template, $title, $content, $options );

When we develop an application based on the standard MVC, we created the known bootstrap (also known as init), which is a class that "flame" the right controller, as per user request.

As I noted, you are calling the controller/method in the file curso.php, what makes this one bootstrap be that file.

As a solution, I can suggest the following:

Develop your bootstrap so that it is dynamic. The controller/method can be passed via parameter (Ex.: http://meu.site.com/index.php?controlador=cursos&metodo=ver) or via URL friendly (Ex.: http://meu.site.com/cursos/ver - requires specific Apache configuration / .htaccess).

In most MVC diagrams this is omitted, being considered as part of the controller. But it’s not! She’s responsible for calling the controller and the right method, as already said.

Another thing you might consider is using a Autoloader to load your classes. I recommend the PSR-4: Autoloader - PHP-FIG.

Since you’re not the one asked, I won’t go into more detail about the pattern MVC (how to structure, what to develop in each place and etc). I wanted to explain this part because it refers to the context of the question. However, I suggest you deepen your study/development on top of what I’ve given you.

Browser other questions tagged

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