Want to ask some router, MVC, CLASS. And so on.

I"ve asked similar questions before, but I didn"t make it clear how I developed it. And now I would like to continue to ask some questions I have seen recently. Let me first introduce my current development environment and some practices. I would like to ask experienced people to help me improve my program.
itself is currently developed in mysqli (mariadb) + php7 + jquery3 mode, but I still don"t know much about the operation of router, MVC architecture, CLASS NAMESPACE and autoload,CURL. I don"t know which is the right thing to do. From the last question to now, I have seen a lot of the implementation of other YOUTUBE masters, and found that everyone"s approach is quite different, resulting in I do not know which approach is right and suitable for me. Although I have tried a lot of new ways to write
, such as OOP mysqli connection, I still can"t figure out how to replace what I have in mind for new ideas, because I know it may be difficult to maintain and collaborate with others if I continue to write.
Let me explain my development method in detail first. Please teach me how to change my practice smoothly with experience

at present, a web page is a PHP file, each file will introduce a PHP file (header.php, footer.php), footer.php is mainly to place the introduction of JS, or according to the name of the file to introduce the required js, and a variety of simple or complex jquery grammar files.
header.php introduces the file is extremely complex, there is a folder where there are many sql statements, will be classified, each php will have a switch, according to which file path to introduce which sql statements, which also contains a file is many function, although there is a general plan to introduce the function, in the right file, but in fact it is still far from class + namespace. (the html header will also be introduced in this file)
there is a file that env.php, will display the correct environment variables according to host.
sql connect.php is currently introduced on every web page, although every web page will need to connect to the database, but this does not seem to be quite right, since understanding OOP.
there is a file that interacts with the back-end database, called update.php, which also uses switch + type to find the right behavior based on the GET/type I gave. The front-end ajax takes a type to update.php to find the right request, and does different things according to different type. Sql connect.php, necessary function and env.php will also be introduced into update.php.
there is a css.php that introduces all the required css files and, if necessary, different css depending on the file.
I referred to the writing of the great god on github. For example, https://github.com/bramus/router, https://github.com/phalcon/mvc, https://github.com/php-curl-c. curl-class, https://github.com/ThingEngin.. It"s a new world, but it suffers from the fact that the current way of writing is a far cry from theirs.

so I would like to ask me how can I improve the way I put it, or even make it MVC and class? My goal is to make people understand my code and work together. I"m sure it"s not easy. But I"m sure it didn"t go so well at the beginning of Facebook. There are so many things that need to be changed, but I don"t know where to start. Let"s start with the ones that are easiest to change. I would like to invite experienced people to give me some advice. Thank you very much. I will add it from time to time!
Welcome to add a lot of l i n e friends (in my profile). If you have any questions, you will be given priority.
I am ready to improve my code.

question add:

    What is the difference between the introduction of
  1. namespace + class and a bunch of function written in the same php and then introduced at once? Suppose you have a lot of function, some for back-end needs and some for front-end display needs, what would you do?
  2. what"s the difference between introducing sql connect, on every page and OOP connect?
  3. Management of js? Suppose there are many click, mouse events in the same js file, would you write it in the same js file?
  4. I put multiple ajax function, in the same js file and use type to decide which ajax to look for. What would you do if it were you?
  5. css,js will you introduce all css,js in the first place? Or in what way can it be separated effectively? To make it appear again when it is time to appear, I am using the switch method, what would you do?
  6. is my update.php practice good? What would you do?
  7. in a php file, basically two files will be introduced. Is it possible for header.php, footer.php, to do this?
Dec.15,2021

took a look at the technology you mentioned. It feels like it has been out of date for a long time.
1. First of all, let's talk about the first problem. This is the problem of lazy loading autoload, which is loaded only when this class is used, otherwise it will not be added. This can get rid of unnecessary loading, and at the same time, it will make the code less imported. The problem that will look a lot better
2.sql is the biggest. In fact, it should be used to connect the sql, without connecting. Please refer to the MVC mode (of course, This MVC is also out of date)
3.js must be a page, a js, and then a public.js or common.js
4 with a public method. In fact, this problem is the problem that can be solved by MVC mode
5. Ditto, refer to MVC mode
6. I have never seen this kind of coquettish operation. I always use router to decide which file to access and what function
7. It's not very available. It's an autoload problem. There's already a solution. Check out


for composer.

I think what you need to do now is to find a PHP MVC framework (such as Yii or Laravel), to ReFactor your project with this framework, remember not to use your previous code; some of the above questions will not be answered until you are familiar with the operation mode of MVC.

it's like in "Slam Dunk". After Hanamichi Sakuragi joined the team, he had to kneel down and pat the ball honestly to practice his feel.

in addition, reading this part, I think someone recommended "Modern PHP" before? In your current state, I think it is more appropriate to read frame practical books, otherwise the more new terms you save, the more anxious you will be, because you don't know how to use them, you might as well write a set of MVC websites in a down-to-earth manner, which will have a greater sense of achievement. In addition, when writing, you should be exposed to things such as ORM, middleware, routing, and so on, and then you can start to read the "Enterprise Application Architecture pattern" (the English original name: Patterns of Enterprise Application Architecture), is about why there are M, V, C layers and architecture patterns. After doing practical exercises, these things will feel good to read. The significance of the pattern is that it is the optimization practice of some specific problems. As for what autoload, OOP and so on, most modern languages will have, do not worry too much about those language features.

the words of a family are for reference only.


in fact, the previous one has been answered clearly. If I see an invitation today, I would like to add. My previous development method is the same as yours, as the high ticket answer said, in fact, it has expired.

most of the causes of your questions are conceptual, and a single file (file) affects your way of thinking as a request.

very easy to solve, learn Modern PHP this book, which introduces modern PHP knowledge such as Composer, or learn a framework, a good foundation can directly learn Laravel, if you do not understand for a while, you can watch the video explanation or start with the simplest MVC framework codeigniter.

regardless of the architecture, the modern framework is single entry , and then route and distribute according to the request, and then go to the request processing, which may be a simple controller layer, or nesting a service layer, and finally return the response to the client after processing. There may be lifecycle hooks in the middle, or middleware does middleware processing.

in the front-end section, it is recommended to abandon the back-end template rendering and learn react or vue. In addition, if you don't like PHP, very much, you can turn to Node.js

.

feels like you're building a frame.

Menu