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.
- IT / Misc 08.07.21 How to make a free translation for a website without an API, translate documents in Google Translate
- IT / Misc 06.07.21 How to make a subscription button on a website, a subscriber base and automatic mailing
- Food / Misc 06.07.21 How to quickly cook delicious fried pies with potatoes and onions
- IT / Misc 04.07.21 Caching - create, load and reset. Where to store the cache, methods and types of caching
- IT / Database 03.07.21 Custom NoSQL - storing data in files and not only in a database. Storing settings, small data and caching files