F3-fatfree small php framework course (III)

Source: Internet
Author: User
Tags autoload
F3-fatfree small php framework tutorial (III) previously said a lot of routing settings and use, here first let's talk about how to set the root directory (the so-called root directory is the localhost directory) generally, the default directory is available when apache is installed. if you want to change the Directory, run the following command: php-Slocalhost: 80-tvarwww, and then the Index is automatically entered. php inside find something (automatically identify whether it is routing or folders) but F3-fatfree small php framework tutorial (3)

I have mentioned many routing settings and usage before. here I will first explain how to set the root directory (the so-called root directory is the localhost directory)

Generally, the default directory is available when apache is installed. to change the Directory, run the following command:

php -S localhost:80 -t /var/www/
Then it will automatically go to Index. php to find something (automatically identify whether it is a route or a folder)


Let's look back at the reroute function just now. This function is used to jump to the page, and it has a special purpose and won't display the source page! That is to say:

$f3->route('GET|HEAD /obsoletepage',    function($f3) {        $f3->reroute('http://www.anotherexample.org/');    });
"/Obsoletepage" is displayed in the browser's address bar, instead of the actual address "http://www.anotherexample.org/#. you can also find the hidden address. Users are not allowed to know the actual address they actually accessed, which can be used to grab the page views. There are some bad ideas ~ Let's just think about it ~


Note that if I want to pass a parameter GET [email protected], [email protected]?, However, the default parameter is null.

We need to write some error reporting mechanisms on our own.

$f3->error(404);

F3 has a good automatic reading mechanism that will help you read classes when you need classes. you do not need to write a lot of include or required to read php files in different folders and files. The party will help you solve this problem. You only need to save the file in the folder. once you want to call the method in the object, you just need to tell the framework to automatically read the appropriate file:

$f3->set('AUTOLOAD','autoload/');
This set function has two parameters. The previous parameter defines 'autoload', which indicates that I want to perform automatic reading (this set function is very suspended and will continue to explain other functions later ), then, the automatically read directory is 'autoload/', which means you have already entered this directory.

You can also set multiple automatic paths.

$f3->set('AUTOLOAD','admin/autoload/; user/autoload/; default/');
The class name must be the same as the file name, so that autoload can read the class from the file. In addition, f3 is case-insensitive, so it is best not to use folders and files with the same name.


The following describes how to read a file in a path:

What I want to read is/var/www/html/autoload/gadgets/ipad. php.

First, the root directory we define is/var/www/html/, and then

$f3->set('AUTOLOAD','autoload/');$obj=new Gadgets\iPad;

The definition in the file is as follows:

namespace Gadgets;class iPad {}

The Gadgets inside is naturally the same as the path, mainly to facilitate autoload identification. after all, the framework is case insensitive. Note that these paths must end with a slash:

$f3->set('AUTOLOAD','main/;aux/');
Before talking about the following concepts, I will first talk about static and dynamic functions:

Static functions: the program disappears at the end of the process. it keeps occupying the memory for calling and can be used without class instantiation.

Dynamic functions: these functions are automatically free when they are dynamically loaded into the memory. However, a new class is required before they can be called in the class.

Literally, if you only need this function and need to be used frequently, use static. if you want the function of the entire class to be called, dynamic


Continue:

How do static and dynamic methods in F3 be called?

Static:

$f3->set('AUTOLOAD','classes/');$f3->route('GET|POST /','Main\Home::show');
Dynamic:

$f3->route('GET|POST /','Main\Home->show');
Classes/main/home. php all contain a namespace named Main, which is not detailed. The show function is called dynamically and statically.

























Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.