您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
webman-視圖-blade視圖模板的使用
发布时间:2022-01-23 09:51:39编辑:雪饮阅读()
上次瞭解了twig模板的使用,那麽這次來瞭解下blade模板的使用,blade模板視圖是laravel框架中主要用的視圖文件,那麽webman中也是可以使用該模板的。
首先安裝blade代碼包
[root@localhost webman]# /usr/local/php734/bin/php -c /usr/local/php734/lib/php.ini /usr/bin/composer require jenssegers/blade ~1.4.0
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? yes
./composer.json has been updated
Running composer update jenssegers/blade
Loading composer repositories with package information
Updating dependencies
Lock file operations: 19 installs, 0 updates, 0 removals
- Locking doctrine/inflector (2.0.4)
- Locking illuminate/bus (v8.80.0)
- Locking illuminate/collections (v8.80.0)
- Locking illuminate/container (v8.80.0)
- Locking illuminate/contracts (v8.80.0)
- Locking illuminate/events (v8.80.0)
- Locking illuminate/filesystem (v8.80.0)
- Locking illuminate/macroable (v8.80.0)
- Locking illuminate/pipeline (v8.80.0)
- Locking illuminate/support (v8.80.0)
- Locking illuminate/view (v8.80.0)
- Locking jenssegers/blade (v1.4.0)
- Locking nesbot/carbon (2.55.2)
- Locking psr/simple-cache (1.0.1)
- Locking symfony/finder (v5.4.2)
- Locking symfony/polyfill-php80 (v1.24.0)
- Locking symfony/translation (v5.4.2)
- Locking symfony/translation-contracts (v2.5.0)
- Locking voku/portable-ascii (1.5.6)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 19 installs, 0 updates, 0 removals
- Downloading voku/portable-ascii (1.5.6)
- Downloading symfony/translation-contracts (v2.5.0)
- Downloading symfony/polyfill-php80 (v1.24.0)
- Downloading symfony/translation (v5.4.2)
- Downloading nesbot/carbon (2.55.2)
- Downloading illuminate/macroable (v8.80.0)
- Downloading psr/simple-cache (1.0.1)
- Downloading illuminate/contracts (v8.80.0)
Failed downloading illuminate/contracts, trying the next URL (404: The "https://mirrors.aliyun.com/composer/dists/illuminate/contracts/5e0fd287a1b22a6b346a9f7cd484d8cf0234585d.zip" file could not be downloaded (HTTP/1.1 404 Not Found))
- Downloading illuminate/contracts (v8.80.0)
- Downloading illuminate/collections (v8.80.0)
Failed downloading illuminate/collections, trying the next URL (404: The "https://mirrors.aliyun.com/composer/dists/illuminate/collections/ed630f15e937c5c928a9454748ff727ce5ea01ec.zip" file could not be downloaded (HTTP/1.1 404 Not Found))
- Downloading illuminate/collections (v8.80.0)
- Downloading doctrine/inflector (2.0.4)
- Downloading illuminate/support (v8.80.0)
Failed downloading illuminate/support, trying the next URL (404: The "https://mirrors.aliyun.com/composer/dists/illuminate/support/69afbd89e6b6bf847e3df59e0f72cd820b18e8da.zip" file could not be downloaded (HTTP/1.1 404 Not Found))
- Downloading illuminate/support (v8.80.0)
- Downloading illuminate/container (v8.80.0)
Failed downloading illuminate/container, trying the next URL (404: The "https://mirrors.aliyun.com/composer/dists/illuminate/container/e18f8ce24a551e086621b00c7b75d6914d2011b4.zip" file could not be downloaded (HTTP/1.1 404 Not Found))
- Downloading illuminate/container (v8.80.0)
- Downloading illuminate/pipeline (v8.80.0)
- Downloading illuminate/bus (v8.80.0)
Failed downloading illuminate/bus, trying the next URL (404: The "https://mirrors.aliyun.com/composer/dists/illuminate/bus/917798f4a21c5eed1f83f9b434ce94f9c4fa8432.zip" file could not be downloaded (HTTP/1.1 404 Not Found))
- Downloading illuminate/bus (v8.80.0)
- Downloading illuminate/events (v8.80.0)
- Downloading symfony/finder (v5.4.2)
- Downloading illuminate/filesystem (v8.80.0)
Failed downloading illuminate/filesystem, trying the next URL (404: The "https://mirrors.aliyun.com/composer/dists/illuminate/filesystem/73db3e9a233ed587ba54f52ab8580f3c7bc872b2.zip" file could not be downloaded (HTTP/1.1 404 Not Found))
- Downloading illuminate/filesystem (v8.80.0)
- Downloading illuminate/view (v8.80.0)
Failed downloading illuminate/view, trying the next URL (404: The "https://mirrors.aliyun.com/composer/dists/illuminate/view/d1556d48872380623a03e99e3053903d7e3a2b33.zip" file could not be downloaded (HTTP/1.1 404 Not Found))
- Downloading illuminate/view (v8.80.0)
- Downloading jenssegers/blade (v1.4.0)
- Installing voku/portable-ascii (1.5.6): Extracting archive
- Installing symfony/translation-contracts (v2.5.0): Extracting archive
- Installing symfony/polyfill-php80 (v1.24.0): Extracting archive
- Installing symfony/translation (v5.4.2): Extracting archive
- Installing nesbot/carbon (2.55.2): Extracting archive
- Installing illuminate/macroable (v8.80.0): Extracting archive
- Installing psr/simple-cache (1.0.1): Extracting archive
- Installing illuminate/contracts (v8.80.0): Extracting archive
- Installing illuminate/collections (v8.80.0): Extracting archive
- Installing doctrine/inflector (2.0.4): Extracting archive
- Installing illuminate/support (v8.80.0): Extracting archive
- Installing illuminate/container (v8.80.0): Extracting archive
- Installing illuminate/pipeline (v8.80.0): Extracting archive
- Installing illuminate/bus (v8.80.0): Extracting archive
- Installing illuminate/events (v8.80.0): Extracting archive
- Installing symfony/finder (v5.4.2): Extracting archive
- Installing illuminate/filesystem (v8.80.0): Extracting archive
- Installing illuminate/view (v8.80.0): Extracting archive
- Installing jenssegers/blade (v1.4.0): 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
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
18 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
18 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
然後我們還需要安裝phpdotenv,否則會讀取.html後綴作爲模板而不是.blade.php作爲後綴進行讀取。
[root@localhost webman]# /usr/local/php734/bin/php -c /usr/local/php734/lib/php.ini /usr/bin/composer require vlucas/phpdotenv ^4.0
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? yes
./composer.json has been updated
Running composer update vlucas/phpdotenv
Loading composer repositories with package information
Updating dependencies
Lock file operations: 2 installs, 0 updates, 0 removals
- Locking phpoption/phpoption (1.8.1)
- Locking vlucas/phpdotenv (v4.2.2)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
- Downloading phpoption/phpoption (1.8.1)
- Downloading vlucas/phpdotenv (v4.2.2)
- Installing phpoption/phpoption (1.8.1): Extracting archive
- Installing vlucas/phpdotenv (v4.2.2): Extracting archive
> support\Plugin::install
> 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'=>Blade::class
];
然後\app\view\user\hello.blade.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'=>Blade::class
];
路由配置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,blade,視圖,模板,使用