New usage of ThinkPHP3.1 widgets

Source: Internet
Author: User
This article mainly introduces the new usage of ThinkPHP3.1 widgets. For more information, see ThinkPHP.

ThinkPHP3.0 does not provide sufficient support for Action and View. it can only implement basic template rendering output. The multi-layer MVC feature introduced by the release of version 3.1 brings us new ideas for implementing widgets, and how to implement it.

Because ThinkPHP3.1 has added support for multi-layer MVC, version 3.1 can support multi-layer controller functions, so we can add another layer on the controller layer: Widget layer.

First, create a Widget Directory under the Lib Directory of the project and create a TestWidget class (Lib/Widget/TestWidget. class. php) as follows:

class TestWidget extends Action{  public function hello($name=''){    echo ("hello,".$name."!");  } }

We can see that the difference between TestWidget and the previous one is that it does not inherit the Widget class, but directly inherits the Action class, which means that the Action method can be directly called in TestWidget, including rendering and output of the template.

How can we call this Widget after the definition is complete? The W method certainly won't work. this time, the R method is required.
The role of the R method is to remotely call the operations of the module, but 3.1 gives it a new role and supports calling the operation methods at all controller layers. therefore, we can call the Widget in the template as follows:

{:R('Test/hello',array('ThinkPHP'),'Widget')}

You can achieve output in a certain area of the page:

hello,ThinkPHP!

Since other controller layers except the Action Controller cannot be accessed directly through URLs, this Widget method can only be called internally through the R method.

You can call Model in the TestWidget class to output other data. if you need to render your own template, you can directly call the display method.

class TestWidget extends Action{  public function hello($name=''){    $this->assign('name',$name);    $this->display('Test:hello');  } }

In the Tpl/Test/directory of the project, create a hello (Tpl/Test/hello.html) template file and add the output:

Hello,{$name}!

If you want to put 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 time, you can put the defined hello template file under the Widget/Test/directory.

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.