Today is the first time to learn PHP. Since the backend is written in PHP, and it uses the open source framework PHPCMF, I have to give it a try,
But writing the backend in PHP is really convenient and faster, compared to Java. Be more relaxed and put aside other factors first. Just want speed, just test
Maybe I am just starting to learn PHP half-way, and have not even learned the grammar. I have direct contact with PHPCMF, so I first find someone familiar with the use of the framework
How to quickly build a module with PHPCMF is what I care about most.
Okay, enough nonsense, let’s get straight to the point.
First import the phpcmf official download package into the zend editor. If there is an error, don’t worry about it. If there is a x sensitive person, there is nothing you can do, as shown below:
We are also concerned about it Just the above four folders, you can start quickly, don’t worry about the others for now,
(1) application folder
If I need to make a news module now, the first thing we need to do is Create a News folder under the application, as follows:
The other folders are all built-in, so don’t worry about them. Next, we need to know what else we need?
Controller, yes, it is a controller. This is very important in PHP. If you don’t know how to build it, if you are just starting out like me, then you can look at any other file under the application Folder, such as User There is a Controller folder under it. We also need to create such a folder
See, we only need to create the same folder Controller, and don’t worry about the others, such as Conf and Menu folder are currently not used, so don’t worry about it. After the folder is created, we need to write the interface next, which is to create a php file under the Controller folder. However, the interface is divided into front-end and back-end, such as news There are front-end displays, back-end editing, additions, deletions, etc. We need to care about this, and there are certain requirements for naming.
Look at the News. Below are the two php files I created. First, let’s talk about the naming rules. Regarding the problem,
First of all, you can give it a random name, and then add Controller.class. If the interface is a background interface, then you need to add AdminController.class. This is very important,
What you need next What I know is how to write this class file,
The first step is that the namespace must be present, the namespace module name is Controller
The second step is to declare who the parent class is, if the interface is the front end Yes, you use HomeBaseController. If the interface is background, you use AdminbaseController, as shown in the figure below:
The third step is to create a class, class NewsController extends HomeBaseController{}
The fourth step is to write the method in the class, which is the interface. For example, what I wrote above is getNewsList(). Finally, just echo the returned data
At this point, the application is half over. There is nothing, as shown in the picture I also drew a circle, which is the address to access this interface. Its structure is also easy to understand
// www.fsfd.com/XXXX/index.php?g=News&m=News&a=getNewsList
Among them, www.fsfd.com/ is the domain name or public IP, XXXX/ is the project name, which is the project name, index.php?This is fixed, there is this file under the project name, you don’t need to change it What, g=Newsthis represents which folder under the application, that is, which module, &m=NewsThis code is the first paragraph of the name of the php file you created under the Controller folder (NewsController. class.php), you can choose this name casually, &a=getNewsList represents the methods below this class. Next, we need to be familiar with the other half of the application that we are more related to, database,
There is a Common folder under the application file, and there is a Model folder under it, that is This Model folder is what we need to care about. In fact, each model file below represents a table in the database,
Let’s first take a look at the models it comes with, such as User
The first step is to namespace, namespace CommonModel;
The third step is to define the class UsersModel extends CommonModel{}. You don’t need to worry about the things inside. You can see that under User is data verification and time conversion. It seems to process passwords. Anyway, I I can’t understand it now. It seems that we don’t need to think about the field definitions needed to connect data. Wait,
Then next we need to write our own, it’s very simple. It’s just a statement
That’s it. PHP itself will match the one in MySQL. The way it parses is like this, and the data table has a The prefix, followed by the lowercase table name, together form the name of a database table. For example, the table in my database is wust_news, and then under the Model, you must be consistent with the second half of the database table field. , it’s OK to just capitalize the first letter, so it will be automatically parsed and matched successfully,
The application is over now
There is actually nothing to say about this. You can tell from the English name that data is placed under this folder, such as cache data. Under the runtime file, the pictures you uploaded in the background management interface Ah, videos, etc., you can create a folder here. Under the conf folder is the routing settings. I don’t know the details. It seems that you don’t need to set it manually. There is no need to say anything specifically here. What
(3) tpl folder
The folder where the front-end templates are placed, you will understand as soon as I say it. For example, when you open a web page, the displayed things are placed in this Yes, the main thing is to look at its hierarchical structure
This is mainly to look at what modules are under the application. Compared with that module, there must be a Controller folder under it, so there is also a Controller folder under the tpl folder. This is it, one is for the client and the other is for the mobile phone. Let’s take a look at what’s under User. Pay attention to the names being the same,
The red × above does not mean an error, it’s just imported. Ignore it, the code we finally write is to be uploaded to the server, so it is correct. If there is a sensitive person, there is nothing we can do. Haha, first we need to look at the Controller under the User folder under the application folder. Let’s just look at a class, CenterController personal center, and pay attention to the two situations $this->display(); and $this->display(':center');. If you use: , then there must be center.html,
, one level below the User folder under tpl. If it is $this->display();, then first you need to see what the name of the controller is What, then go to User and create a folder with the first half of the name, capitalize the first letter, and then below, you can see the html file consistent with the method name,
It should be very clear from the picture. This part is over now, (4) tpl_admin background management module
Please see this part (3) Here is where the background management things are specially placed. The situation is the same as the tpl folder, so I won’t repeat it. Book it
Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces PHPCMF+php+diary (1), including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.