Use require or directly call the *.php file

Asked

Viewed 345 times

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 user navigation is done through secao=questionario, secao=cadastrar..., thus browsing the entire site and done without directly accessing the other pages only with the use of require.

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

My question is which would bring me better performance, maintain access only to index.php with the use of require or direct access to files ?

1 answer

1


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

From a performance standpoint, I can’t say. I imagine there’s no reasonable gain, because there’s code you’ll need to execute before any content, so the separate pages would have some require on top anyway.

From the point of view of facilitating maintenance, what you are doing is much better, because you focus execution at this starting point always, rather than having multiple starting points on the whole system. If it’s 10 pages, there’s not so much problem, but if it’s 40 it’s complicated enough, imagine if you solve that some of the pages will run a database access and others will not, then you’ll stay in that sea of crazy and unnecessary includes, your life is a hell.

I personally like to use a micro-framework called Fat-Free-Framework that already comes with a route scheme that makes this workflow a lot easier. https://fatfreeframework.com/3.6/home

Just remember that when using such a router you need to activate Modrewrite in Apache (or .htaccess) and always have it 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]
  • Your answer is really very good. A framework with routes always seems to me a much better maintenance option than a lot of includes.

  • 1

    Thank you very much, for the tip of F3 still studying this framework, I will try to go studying and posting the contents to help the community.

  • You’re welcome, brother. Just an addendum: I had several mistakes when using his Mapper in a SQL Server 2003 database, so it turns on that they are not mega compatible. Other than that, I’m glad you’re helping the community. I had to give them a helping hand myself, because this framework helps me a lot.

  • @Daniel I’m studying this entire framework to realize that there are many errors regarding its documentation and consequently getting worried about its functionalities. for example the function $f3->parse(); that just doesn’t work.

  • Oops, yeah, like I said, you got your problems. In the end, it’s open-source, so the solution is to participate in development when you realize something doesn’t work. Of course, there are many other micro-frameworks out there (Silex, Slim, etc...) so you don’t have to get stuck in that. This parse function I never used, but worth taking a look at the source code maybe.

Browser other questions tagged

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