您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
webman-視圖-think-template視圖模板的使用
发布时间:2022-01-24 11:00:56编辑:雪饮阅读()
上次瞭解了blade模板的使用,那麽這次來瞭解下think-template模板的使用,think-template一直作为ThinkPHP的内置模板引擎,现已经支持独立使用。
首先安裝think-template代碼包
[root@localhost webman]# /usr/local/php734/bin/php -c /usr/local/php734/lib/php.ini /usr/bin/composer require topthink/think-template
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? yes
Using version ^2.0 for topthink/think-template
./composer.json has been updated
Running composer update topthink/think-template
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
- Locking topthink/think-template (v2.0.8)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Downloading topthink/think-template (v2.0.8)
- Installing topthink/think-template (v2.0.8): Extracting archive
> support\Plugin::install
Generating autoload files
20 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
然後\app\controller\Foo.php控制器實現如:
<?php
namespace app\controller;
use support\Request;
class Foo
{
/**
* 该方法会在请求前调用
*/
public function beforeAction(Request $request)
{
echo 'beforeAction';
// 若果想终止执行Action就直接返回Response对象,不想终止则无需return
// return response('终止执行Action');
}
/**
* 该方法会在请求后调用
*/
public function afterAction(Request $request, $response)
{
echo 'afterAction';
// 如果想串改请求结果,可以直接返回一个新的Response对象
// return response('afterAction');
}
public function index(Request $request)
{
return view('user/hello', ['name' => 'webman111']);
}
public function login(Request $request)
{
$session = $request->session();
$session->set('userinfo', ["user_name"=>"kasumi"]);
return response('login success');
}
public function loginPage(Request $request){
return response("this is loginPage \n");
}
}
然後config/view.php中實現如:
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use support\view\Raw;
use support\view\Twig;
use support\view\Blade;
use support\view\ThinkPHP;
return [
//'handler' => Raw::class
'handler' => ThinkPHP::class
];
然後\app\view\user\hello.html模板創建創建如:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>webman</title>
</head>
<body>
hello ggg {$name}
</body>
</html>
路由配置config/route.php:
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use Webman\Route;
Route::get('/blog/index', [app\controller\Foo::class, 'index'])->middleware([app\middleware\AccessControlTest::class]);
然後服務啓動
[root@localhost webman]# /usr/local/php734/bin/php -c /usr/local/php734/lib/php.ini start.php start
Workerman[start.php] start in DEBUG mode
----------------------------------------- WORKERMAN -----------------------------------------
Workerman version:4.0.26 PHP version:7.3.4
------------------------------------------ WORKERS ------------------------------------------
proto user worker listen processes status
tcp root webman http://0.0.0.0:8787 4 [OK]
tcp root monitor none 1 [OK]
---------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
然後訪問如:
[root@localhost ~]# elinks http://127.0.0.1:8787/blog/index --dump
hello ggg webman111
关键字词:webman,think-template,視圖,模板,使用
下一篇:webman-視圖-模板賦值