Home > Backend Development > PHP Tutorial > About the usage of Widget in ThinkPHP3.1

About the usage of Widget in ThinkPHP3.1

不言
Release: 2023-04-01 14:28:01
Original
1710 people have browsed it

This article mainly introduces the new usage of Widget in ThinkPHP3.1. Friends who need it can refer to it

The support of Widget in ThinkPHP3.0 version for Action and View is relatively insufficient and can only be realized Basic template rendering output. The multi-layer MVC function brought by the release of version 3.1 has brought new ideas to the way we implement Widgets. Let's see how to implement it.

Since ThinkPHP3.1 adds support for multi-layer MVC, version 3.1 can support multi-layer controller functions, so we can add another layer to 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."!");
  }
 }
Copy after login

We 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. This means that the Action method can be directly called in TestWidget, including the rendering output of the template.

After the definition is completed, how do we call this Widget? The W method will definitely not work. This time the R method is needed.
The function of the R method is to remotely call the operation of the module, but 3.1 has given it a new role and can support calling the operation methods of all controller layers. Therefore, we can call the Widget in the template like this:

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

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

hello,ThinkPHP!
Copy after login

Because in addition to Action control Other controller layers outside the controller cannot be accessed directly through the URL, so 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 just call the display method directly.

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

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

Hello,{$name}!
Copy after login

If you want to put the template file in 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');
  }
 }
Copy after login

At this time, you can put the hello template file you just defined into the Widget/Test/ directory.

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Method to export to Excel or CSV based on php

##

The above is the detailed content of About the usage of Widget in ThinkPHP3.1. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template