Functions you must know when entering Phalcon 'Phalcon Entry Guide Series 2'

咔咔
Release: 2023-04-10 10:24:02
Original
2821 people have browsed it

Let us learn Phalcon through examples

  • Contents of this series
  • Preface
  • 1. Project structure
  • 2. Entry file
  • 3. Configuring Nginx
  • 4. Controller jump
  • 5. Database addition, deletion, modification and query
    • Insert data
    • Modify data
    • Delete data
  • 6. Code optimization
  • Summary

Directory of this series

1. Installing Phalcon on Windows "Phalcon Pit Guide Series 1"

Preface

The previous article introduced you to the installation of Phalcon and the use of Phalcon development tools Created projects, controllers, and models. Just did a few simple operations.

In this issue, we will continue to talk about the actual use of Phalcon.

1. Project structure

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

From As can be seen from the above figure, this directory structure is very similar to the TP framework. The corresponding directories will not be explained one by one. Let me tell you about the migrations directory.

This directory is just like the database migration in laravel. I won’t go into details on how to use it!

The framework structure is not fixed. Like ThinkPHP, you can register a namespace to modify the directory structure.

In the Phalcon framework, Kaka’s recent project was also developed using multiple modules. However, the directory structure is also different from the directory generated using the Phalcon development tool.

Everything remains the same, they all look the same.

2. Entry file

An essential file for each framework, index.php seems to be all Developer default.

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

Then it is also essential in the framework of Phalcon.

I won’t analyze the source code in detail about what is loaded here. It is not necessary. If you want to see the source code analysis, you can search for ThinkPHP framework source code analysis.

The general execution is to perform dependency injection first, and use /config/services.php to introduce some files. What you need to pay attention to is that the database connection is made here.

This file/config/router.phpYou will know what it is by looking at the name, routing! How to set up routing will be discussed later.

Get the configuration information after passing the first step of dependency injection.

The last line of code is include APP_PATH . '/config/loader.php';Register the directory obtained from the configuration information.

3. Configure Nginx

In the first issue of the article, the project was not configured. Let’s do a simple configuration next.

Phalcon provides three ways of configuration, let’s just use the simplest one first.

server {
        listen        80;
        server_name  www.kakaweb.com;
        root   "D:/phpstudy_pro/WWW/phalcon/public";
        index index.php index.html error/index.html;
	    location / {
	        try_files $uri $uri/ /index.php?_url=$uri&$args;
	    }

        
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9002;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
	
	    location ~ /\.ht {
	        deny all;
	    }}
Copy after login

The above is the configuration of Kaka. If you are also using PhpStudy, you can directly copy it and use it.

4. Controller jump

In the first article, the control was created using the phalcon development tool If you haven't created a project yet, you need to read the first article!

Let’s see how the visit goes first.

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

Code

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

You can see that in the index controller, another method kaka is also established.

Mainstream frameworks are configured with the index controller as the default access path. How to access this kaka is the same as other frameworks.

The access link is http://www.kakaweb.com/index/kaka.

is the domain name controller method name. It should be noted that the method name here does not need to include Action.

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

Practice the official case.

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

You can see that the output result is a link

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

This link will jump directly to the Signup controller. Next, use the developer tools to generate this controller.

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

Then click the button just now and it will jump to the Signup controller.

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

Let’s talk about the controller first.

5. Database addition, deletion, modification and query

You can see that it is defined in advance in the model file Okay, two methods, no matter what they are, let’s try them first.

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

Write the following code directly in the controller

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

##Query results

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

It can be seen that

    find method is to get all the data
  • findFirst only takes the first piece of data
  • find(15) Query the data with id 15
  • find("type = 'mechanical'"); Conditional search

Insert data

实现代码

    public function holdAction ()
    {
        $user = new User();

        $phql = "INSERT INTO User (name, age, sex) VALUES (:name:, :age:, :sex:)";

        $status = $user->modelsManager->executeQuery($phql, array(
            'name' => "咔咔1",
            'age' => 24,
            'sex' => 1
        ));

    }
Copy after login

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

这里需要注意一下这个SQL语句$phql = "INSERT INTO User (name, age, sex) VALUES (:name:, :age:, :sex:)";

在这里User指的是模型,并不是数据库表名。

修改数据

实现代码

    public function modifyAction ()
    {
        $user = new User();

        $phql = "UPDATE User SET name = :name:, age = :age:, sex = :sex: WHERE id = :id:";

        $status = $user->modelsManager->executeQuery($phql, array(
            'id' => 20,
            'name' => "咔咔2",
            'age' => 25,
            'sex' => 2
        ));
    }
Copy after login

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

    public function deleteAction ()
    {
        $user = new User();

        $phql = "DELETE FROM User WHERE id = :id:";

        $status = $user->modelsManager->executeQuery($phql, array(
            'id' => 20
        ));

    }
Copy after login

可以看到已经没有结果了

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

这时你会发现,在检索数据的时候用的框架自带的方法,到增、删、改使用的类似于原生了。

对于这个问题,如果你是新手建议会那种方式就用那种方式,因为工期可不等你。

Kaka will also talk to you about the method of using framework modifications. Don’t worry about this. The next article will be published!

6. Code Optimization

In Section 5, did you find this problem?

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

In all methods, the User model is instantiated, and this is OK.

But think about it, if you use this method for a full project in the early stages of the project, and find that you need to change the name in the middle, what would you do?

Search the User keyword globally and change it to the modified name?

To be honest, few programmers dare to do this kind of operation because you don’t know where the problem will occur.

So Kaka will tell you a method for unified management of these models.

Functions you must know when entering Phalcon Phalcon Entry Guide Series 2

You can declare the model in your own way.

Then initialize in the controller and instantiate the model here.

At this point you are thinking that if the table name is changed, do we only need to modify the name in the initialization method?

Summarize

This article introduces you to the necessary functions when using a framework.

Although a native-like method is used in the process of adding, deleting, modifying, and checking, this method is rarely used in any framework.

But no matter which way it is, it’s all code, right? Don't sneer at it, the framework functions can change at will, but these SQL statements will never change.

#Persistence in learning, persistence in writing, and persistence in sharing are the beliefs that Kaka has always adhered to since his career. I hope that Kaka’s articles on the huge Internet can bring you a little bit of help. I’m Kaka, see you next time.

The above is the detailed content of Functions you must know when entering Phalcon 'Phalcon Entry Guide Series 2'. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template