Placing content separate from PHP code
When developing, it is often required to indicate in the code any values of variables containing Russian or English text. This text can be called content . In order to ensure easy maintenance of the code in the future, it is necessary to think carefully about the code structure . Namely, according to the MVC concept , content should be placed separately from code. This approach will make it easy to change values, translate text into different languages.
How can you implement this in PHP? The simplest solution is to create a content file next to the logic file. For example, the file with the code will be named admin.php , and next to it, place admin.content . Next, you need to create once the code that will include the content file, as a result of which all content will be available in the file with logic.
The file with the content itself can be represented as a array , the keys of which will be the names of the variables, and the values of the content of this variable itself. Such an array can be multidimensional . In the code, after connecting the content file , you can refer to the array elements by specifying the keys. You can also implement immediately multilingualism , in this case the array may contain keys with the names of languages, inside these keys directly the necessary keys and values.
This code splitting approach allows you to easily replace content values when needed. For example, you need to change the name of a menu item - for this you do not need to search for this fragment in a long logic file, you just need to find the required key and change its values, and next to it you can easily specify a value for another language .
Usage is shown in the screenshot below. One method is created for the entire application, which connects the content file (which you specify, for example, by calling the method with the parameter-name of the content file). In this method, you can implement everything you need, for example, taking into account the application language, which will allow, depending on the site settings, to use the necessary content values. Next, an array from the content file is written to the content variable, after which you can simply access the required values by the key.
Thus, separating content from code is an important part of developing a quality website or web application . This approach is used by the Eqsash CMS , which can be downloaded from the download section on this site.
- 09.11.23IT / Database Errors when migrating from MySQL 5.6 to 5.7 and how to fix them - database dump import failed with an error or INSERT does not work. Disabling STRICT_TRANS_TABLES strict mode or using IGNORE
- 09.07.22IT / Misc Convert office files DOC, DOCX, DOCM, RTF to DOCX, DOCM, DOC, RTF, PDF, HTML, XML, TXT formats without loss and markup changes
- 07.07.22IT / Safety How to protect PHP, JS, HTML, CSS source code - obfuscation, minification, compression and encryption
- 06.07.22IT / Safety Connection not secure, problem with Lets Encrypt - how to fix expired 09/30/2021 DST Root CA X3, remove it manually and install ISRG Root X1. Example on MS Windows 7
- 08.07.21IT / Misc How to make a free translation for a website without an API, translate documents in Google Translate