New usage of ThinkPHP3.1 widgets _php Tutorial

Source: Internet
Author: User
The ThinkPHP3.0 version of the widget has relatively little support for action and view, and only the basic template render output is possible. The multi-layered MVC feature introduced by Release 3.1 gives us a new idea of how to implement widgets, and how to do it.

Since ThinkPHP3.1 adds support for multiple layers of MVC, the 3.1 version can support multiple layers of controller functionality, so we can add another layer to the controller layer: the widget layer.

First, create a Widget directory under the project's Lib directory, and create a Testwidget class (lib/widget/testwidget.class.php) as follows:

Class Testwidget extends action{public  function Hello ($name = ") {    echo (" Hello, ". $name."! ");}  

We see that the difference between testwidget and the previous one is that instead of inheriting the Widget class, it inherits the action class directly, which represents the method that can invoke action directly in Testwidget, including rendering output to the template.

After the definition is complete, how do we invoke the widget? Using the W method will definitely not work, this time we need to play the R method.
The function of the R method is to invoke the operation of the module remotely, but 3.1 gives it a new role that can support invoking the action method of all controller layers, so we can call the widget in the template:

{: R (' Test/hello ', Array (' thinkphp '), ' Widget ')}

You can implement an area output in the page:

hello,thinkphp!

Because the controller layer other than the action controller cannot be accessed directly through a URL, the widget method can only be called internally by the R method.

You can call model in the Testwidget class to output additional data, and if you need to render your own template, you can call the display method directly.

Class Testwidget extends action{public  function Hello ($name = ") {    $this->assign (' name ', $name);    $this->display (' Test:hello ');  } }

We create a hello (tpl/test/hello.html) template file under the project's tpl/test/directory and add the output:

hello,{$name}!

If you want to place the template file under the current directory like the previous widget, you can use:

Class Testwidget extends action{public  function Hello ($name = ") {    $this->assign (' name ', $name);    $this->display (DirName (__file__). ' /test/hello.html ');  } }

At this point, you can put the Hello template file you just defined in the widget/test/directory.

http://www.bkjia.com/PHPjc/825369.html www.bkjia.com true http://www.bkjia.com/PHPjc/825369.html techarticle The ThinkPHP3.0 version of the widget has relatively little support for action and view, and only the basic template render output is possible. And the release of the 3.1 release brings the multi-layered MVC function, gives us real ...

  • 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.