您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
webman-視圖-模板賦值
发布时间:2022-01-24 11:46:01编辑:雪饮阅读()
除了使用view(模版, 变量数组)给模版赋值,我们还可以在任意位置通过调用View::assign()给模版赋值。
View::assign()在某些场景下非常有用,例如某系统每个页面首部都要显示当前登录者信息,
如果每个页面都将此信息通过 view('模版', ['user_info' => '用户信息']); 赋值将非常麻烦。解决办法就是在中间件中获得用户信息,然后通过View::assign()将用户信息赋值给模版。
首先是路由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]);
然後所用的中間件\app\middleware\AccessControlTest.php:
<?php
namespace app\middleware;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
use support\View;
class AccessControlTest implements MiddlewareInterface
{
public function process(Request $request, callable $next) : Response
{
View::assign('name3', 'value3');
$request->data = ["user_name"=>"kasumi"];
$response = $next($request);
return $response;
}
}
然後視圖我們用config/view.php配置為think-template(當然這裏僅僅是以think-template爲例,也可以用其它模板引擎,不同引擎可能有所配置不同):
<?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\controller\Foo.php:
<?php
namespace app\controller;
use support\Request;
use support\View;
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)
{
View::assign([
'name1' => 'value1',
'name2'=> 'value2',
]);
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");
}
}
然後是我們所用到的模板文件\app\view\hello.html:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>webman</title>
</head>
<body>
hello ggg {$name} {$name1} {$name3}
</body>
</html>
我們修改代碼時候其實代碼通過reload已經自動重載到内存了
[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/controller/Foo.php update and reload
Workerman[start.php] reloading
/www/wwwroot/webman/webman/app/controller/Foo.php 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/controller/Foo.php update and reload
Workerman[start.php] reloading
/www/wwwroot/webman/webman/app/middleware/AccessControlTest.php update and reload
Workerman[start.php] reloading
/www/wwwroot/webman/webman/app/middleware/AccessControlTest.php 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 ~]# elinks http://127.0.0.1:8787/blog/index --dump
hello ggg webman111 value1 value3
关键字词:webman,視圖,模板,賦值