您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
webman-視圖-twig視圖模板的使用
发布时间:2022-01-20 23:29:19编辑:雪饮阅读()
webman中除了内置的是Raw視圖,他還支持twig視圖。
首先需要謹記必須在start.php同目錄執行twig安裝命令如:
[root@localhost webman]# /usr/local/php734/bin/php -c /usr/local/php734/lib/php.ini /usr/bin/composer require twig/twig
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? yes
Using version ^3.3 for twig/twig
./composer.json has been updated
Running composer update twig/twig
Loading composer repositories with package information
Updating dependencies
Lock file operations: 3 installs, 0 updates, 0 removals
- Locking symfony/polyfill-ctype (v1.24.0)
- Locking symfony/polyfill-mbstring (v1.24.0)
- Locking twig/twig (v3.3.7)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 11 installs, 0 updates, 0 removals
- Downloading symfony/deprecation-contracts (v2.5.0)
- Downloading psr/http-client (1.0.1)
- Downloading ralouphie/getallheaders (3.0.3)
- Downloading psr/http-factory (1.0.1)
- Downloading guzzlehttp/psr7 (2.1.0)
- Downloading guzzlehttp/promises (1.5.1)
- Downloading guzzlehttp/guzzle (7.4.1)
- Downloading symfony/polyfill-mbstring (v1.24.0)
- Downloading symfony/polyfill-ctype (v1.24.0)
- Downloading twig/twig (v3.3.7)
Failed downloading twig/twig, trying the next URL (404: The "https://mirrors.aliyun.com/composer/dists/twig/twig/8f168c6ffa3ce76d1786b3cd52275424a3fc675b.zip" file could not be downloaded (HTTP/1.1 404 Not Found))
- Downloading twig/twig (v3.3.7)
- Installing symfony/deprecation-contracts (v2.5.0): Extracting archive
- Installing psr/http-message (1.0.1): Extracting archive
- Installing psr/http-client (1.0.1): Extracting archive
- Installing ralouphie/getallheaders (3.0.3): Extracting archive
- Installing psr/http-factory (1.0.1): Extracting archive
- Installing guzzlehttp/psr7 (2.1.0): Extracting archive
- Installing guzzlehttp/promises (1.5.1): Extracting archive
- Installing guzzlehttp/guzzle (7.4.1): Extracting archive
- Installing symfony/polyfill-mbstring (v1.24.0): Extracting archive
- Installing symfony/polyfill-ctype (v1.24.0): Extracting archive
- Installing twig/twig (v3.3.7): Extracting archive
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
Generating autoload files
10 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
否則基本你安裝了twig,并且配置了twig,最後實例啓動並訪問時候有可能報錯Class 'Twig\Environment' not found 之類的錯誤。
筆者這裏就犯了這個低級錯誤。
安裝好之後在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'=>Twig::class
];
這裏我們新增了twig視圖的啓用。
接下來我們config/route.php中新增一條路由到Foo控制器的路由:
<?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]);
這裏中間件其實無所謂配置或不配置了,能達到Foo控制器中index方法即可。
接下來\app\controller\Foo.php控制器中實現index方法調用一個view視圖層:
<?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' => 'webman']);
}
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");
}
}
接下來咱們的視圖\app\view\user\hello.html的實現:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>webman</title>
</head>
<body>
hello ggg {{ name }}
</body>
</html>
這裏{{變量名}}就是twig模板所用的一個簡單的模板語法。
那麽最後就是以上修改來修改去的運行時(及被訪問):
[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.
/www/wwwroot/webman/webman/app/view/user/hello.html update and reload
Workerman[start.php] reloading
/www/wwwroot/webman/webman/app/view/user/hello.html update and reload
Workerman[start.php] reloading
PHP Parse error: syntax error, unexpected '=', expecting end of file in /www/wwwroot/webman/webman/app/view/user/hello.html on line 8
/www/wwwroot/webman/webman/app/view/user/hello.html update and reload
Workerman[start.php] reloading
/www/wwwroot/webman/webman/app/view/user/hello.html update and reload
Workerman[start.php] reloading
/www/wwwroot/webman/webman/app/view/user/hello.html update and reload
Workerman[start.php] reloading
/www/wwwroot/webman/webman/app/view/user/hello.html update and reload
Workerman[start.php] reloading
以上實例被訪問如:
[root@localhost webman]# elinks http://192.168.43.170:8787/blog/index --dump
hello ggg webman
关键字词:webman,視圖,twig,模板