Fatfree-f3 small php Framework (2)

Source: Internet
Author: User
Fatfree-f3 small php Framework (ii) our first example is not too difficult to understand, but if we want to do more features and requirements, we need to go to $ f3-& gt; run () add another route: $ f3-& gt; route (GETabout, function () {echoDonationsgotoalocalcharity... us !; Fatfree-f3 small php Framework (2)

The first example is not too difficult to understand, but if we want to add more features and requirements, we need to add some other routes before $ f3-> run:

$f3->route('GET /about',    function() {        echo 'Donations go to a local charity... us!';    });
If you do not want to confuse global naming, fatfree has different routing ing methods to manage object-oriented classes and methods:

class WebPage {    function display() {        echo 'I cannot object to an object';    }}$f3->route('GET /about','WebPage->display');


Http requirements can also be routed to static class functions:

$f3->route('GET /login','Auth::login');



Routes and symbols:

As a powerful special domain language such as fatfree, you can specify a single routing zone to control different situations:

$f3->route('GET [email protected]',    function($f3) {        echo $f3->get('PARAMS.count').' bottles of beer on the wall.';    });
[Email protected], [email protected] rew/98 ([email protected] value is 98) or/brew/99, then this 98 value will be assigned [email protected], call funtion to output "98 bottles of beer on the wall ".

In addition to numbers, [email protected], for example,/brew/unbreakable, then outputs "unbreakable bottles of beer on the wall ".

There is another trick:

$f3->route('GET [email protected]',    function($f3,$params) {        echo $params['count'].' cars.';    });

Another method is to use "*" to replace any unimportant information, for example:
$f3->route('GET /car/*',    function() {        echo 'We have enough cars.';    });
That is, no matter what you enter in/car/## #%#@ # @ [email protected]


If you want to change the method to another webpage, we can use this method:

$f3->route('GET|HEAD /go_google',    function($f3) {        $f3->reroute('http://www.google.com');    });

That is to say, you can enter http: // localhost/go_google to jump to the page. (Note: these are all defined in index. php. after entering the folder, the system will automatically search for the route in the index and automatically identify whether the route is a folder or not. Don't worry)

The reroute method is used to jump to the page, and the previous colleagues used the GET and HEAD requirements. In addition, there is also a POST, so you can understand their differences.


Name the route:

When you define a route, you can name the route. You can use the routing name and template in the code to replace the URL type. If you want to do this, you need to define the route name at the place where the route is defined, for example

$f3->route('GET @beer_list: /beer', 'Beer->list');
Note that the routing name must use the php naming rules. This example illustrates [email protected]?.

Then, you need to use ALISASES to name the exclusive array. let's look at a call example.

 href="[email protected]_list}}">view beer list

(A href indicates a hyperlink. you can check the html usage)

Of course, this usage can also be used to jump to pages, such as the reroute function just learned:

$f3->reroute([email protected]_list'); // note the single quotes

This is not a jump to google, but to the "/beer" page.

At the same time, our commonly used overload functions are also suitable for this situation, for example:

$f3->route('GET @beer_list: [email protected]', 'Beer->bycountry');$f3->route('GET @beer_list: [email protected][email protected]', 'Beer->byvillage');// a set of key-value pairs is passed as argument to named route$f3->reroute([email protected]_list(@country=Germany)');// if more than one token needed$f3->reroute([email protected]_list(@country=Germany,@village=Rhine)');

The [email protected] _ list variable is overloaded. of course, two different functions, bycountry and byvillage, are called respectively, and then the parameters are given based on the differences between the two methods, [email protected] [email protected], [email protected]? If you give two, you will naturally call the following one.








Modizsiri, 1st Floor, yesterday
Nun

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.