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.
Latest articles
- 03.04.24IT / Уроки PHP Уроки простыми словами. Урок 3. Все операторы PHP с примерами, с выводом работы кода на экран.
- 02.04.24IT / Уроки PHP Уроки простыми словами. Урок 2. Типы данных в PHP с примерами.
- 02.04.24IT / Уроки PHP Уроки простыми словами. Урок 1. Коротко о языке веб-программирования PHP. Основы синтаксиса.
- 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
- 08.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