search
HomeBackend DevelopmentPHP TutorialGetting Started with Thinkphp 2 (46)

【No operation processing】

Look at the following picture:

Actual situation: Our User controller does not have the hello() method

If an object accesses a method that does not exist in this class, then it will access it." Magic method __call()"

  The user accesses a non-existent operation - "Solution: Define an _empty() method for each controller to handle

  The second solution: Define an empty operation

【Empty module processing】

We use a class, but this class has not been included yet.

We can handle __autoload() through the automatic loading mechanism. If the automatic loading mechanism does not find this class, an error will be reported.

That is, request an empty module

Solution one: define an empty controller, empty module

Solution two:

【Add function library file to the application】

In the Common folder Inside, is the place where our function library files are placed

[Module Grouping]

 1.Controller for grouping settings

 2.View templates need to be grouped

 3. Configuration variables need to be grouped

  4. Configure config.php

【Pre-operation, post-operation】

When the method in the controller is called , you can do some additional work before or after the call, which is called pre-operation and post-operation

When we request this URL: http://url/index.php/Admin/Goods/zhanshi, When the method exists, where does this action occur? The pre-operations in the exec() method of App.class.php

can be executed first when zhanshi() is executed. The post-operation can be executed after zhanshi() is executed

There are many methods in a class, all of which require pre- and post-operations. How to solve it?

Solution:

[Cross-module call]

Instantiating a non-existent class will automatically load the class through __autoload().

Where is __autoload() of tp framework? In Think.class.php

instantiate the controller object through the A() method

 A('Module Controller') For example: A('Goods')

 A('Group/Controller') For example: A ('home/Ucenter');

A('project://group/controller') For example: A('shop://home/Ucenter');

There is a nested import call in the A() method () method, this method helps us obtain the corresponding controller and require it to be introduced. The A() method directly new instantiates the object

R ("project://group/controller/operation") method

 which inherits the introduction of the controller, instantiated objects, and method calls.

  R('Module Controller/Operation') For example: A('Goods')

R('Group/Controller/Operation') For example: A('home/Ucenter/members');

R( 'Project://group/controller/operation') For example: A('shop://home/Ucenter/members');

Call the A() method in the R() method

Call in the A() method import() method

 A() method is used

 R() method is used

The above introduces Getting Started with Thinkphp Part 2 (46), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools