Use require or directly call the * .php file

1

Currently my project is following the following structure.

    // SESSAO
if(($SisFuncoes->verificaPermissaoSecao($secao, 'home')) || $secao == null){
    // requerindo a home do site.
    require("html_home.php");        
}
elseif($SisFuncoes->verificaPermissaoSecao($secao, 'cadastrar')){
    require("html_cadastrar.php");
}
//.
//.
//.    
elseif($SisFuncoes->verificaPermissaoSecao($secao, 'admin_usuarios', 3)){
    require("html_admin_usuarios.php");
}
elseif($SisFuncoes->verificaPermissaoSecao($secao, 'questionario', 1)){
    require("html_questionario.php");
}        
else{
    // requerindo a home do site.
    require("html_home.php");
}

These validations are in the index.php file so the user's navigation is done through secao=questionario, secao=cadastrar ..., so navigation is done all over the site and is done without directly accessing other pages only with the use of require.

This is instead of using require and working with "$PHP_SELF/secao=cadastrar" I thought of something similar to the example: "$ PHP_SELF / cadatrar.php" using second mode I will directly access the .php file

My question is which one would bring me better performance, keep access only to index.php with require or direct access to files?

    
asked by anonymous 14.03.2017 / 22:07

1 answer

1

The standard nowadays is to use a friendly URL ( site.com/nome_da_secao/nome_do_recurso ). To do this, it's similar to what you're doing: all addresses access a starting point (the index.php) and it decides what to display.

From a performance standpoint, I can not say. I guess it has no reasonable gain, because there is code that you will need to run before any content, so the separate pages would have some require on top anyway.

From the standpoint of facilitating maintenance, what you're doing is a lot better, why you focus on execution at that starting point always, rather than having multiple starting points in the whole system. If it's 10 pages, it's not that much trouble, but if it's 40 is already quite complicated, imagine if you decide that some of the pages will perform a database access and some not, then you will stay in that sea of crazy and unnecessary includes, Your life becomes hell.

I personally like to use a micro-framework called Fat-Free-Framework that already comes with a routing scheme that makes it easy to work around this flow. link

Just remember that when using such a router you need to enable ModRewrite in Apache (or .htaccess) and have it always run index.php

RewriteBase /

RewriteRule ^(tmp)\/|\.ini$ - [R=404]

RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L,QSA]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
    
14.03.2017 / 22:26