当前位置: 首页 > news >正文

网站建设首先神马移动排名优化

网站建设首先,神马移动排名优化,深圳做微信网站,做购物网站安装运行Hyperf 上回讲到,我们对一个普通的 Laravel 框架进行了改造,让它可以在 Swoole 环境下使用,不过其中会有很多问题可能我们一时考虑不到,就会造成程序的稳定性出现问题。那么,今天我们就来学习一个原生的 Swoo…

安装运行Hyperf

上回讲到,我们对一个普通的 Laravel 框架进行了改造,让它可以在 Swoole 环境下使用,不过其中会有很多问题可能我们一时考虑不到,就会造成程序的稳定性出现问题。那么,今天我们就来学习一个原生的 Swoole 框架 Hyperf 。

这个框架也是国人开发的,并且还一直比较活跃,同时版本也比较新,目前写这篇文章时是 2.2 版本,需要 Swoole 4.5 以上才可以使用,而 PHP 版本需要大于等于 7.4 以上。

另外需要注意的是,运行这个框架要关闭 Swoole 配置中的 use_shortname ,也就是 php.ini 文件中,设置一下 swoole.use_shortname=0 或者 swoole.use_shortname=off 就可以了。大家也可以先不配置,然后看下在启用 use_shortname 时,运行 Hyperf 框架会报什么错。

安装过程非常简单,直接 composer 安装即可。

composer create-project hyperf/hyperf-skeleton 

安装完了之后,看看目录,是不是就已经感觉和 Laravel 非常像了,怎么用呢?很简单,安装完了就可以在根目录执行下面这个命令行语句。

php bin/hyperf.php start

访问一下 localhost 的 9501 端口吧,我们就能看到 Hyperf 的首页已经输出了一段 json 语句。

{"method":"GET","message":"Hello Hyperf."}

恭喜你,安装完成了哦。

PHP8 的过时问题

在我的虚拟机环境中,因为安装的是 PHP8 所以在运行 Hyperf 的时候出现了下面的警告语句。

Deprecated: Return type of Hyperf\Database\Model\Model::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/www/6.框架/hyperf-skeleton/vendor/hyperf/database/src/Model/Model.php on line 1156

这其实是 PHP8 的一个新特性,ReturnTypeWillChange 的意思就是如果你要是实现了一个接口中的方法,那么返回值也要定义成和接口中的方法定义一样的,如果没有定义返回值或者不一样,就会报这种过时警告。其实这不是什么大事,你可以忽略它。如果看得不习惯的话,找到框架中的代码源文件,比如上面的 Hyperf\Database\Model\Model 这个类文件,然后在它的 offsetExists() 方法上加上 #[\ReturnTypeWillChange] 注解,或者给这个 offsetExists() 方法指定返回值就行了。

后面录制视频时,会在本机使用 PHP7 环境,不会出现这个问题。

路由、控制器及视图页面

Hyperf 的路由文件在 config/routes.php 中,可以看到默认首页的路由是这样子的。

Router::addRoute(['GET', 'POST', 'HEAD'], '/', 'App\Controller\IndexController@index');

它指向一个控制器,写法不陌生吧。IndexController 中的内容是这样的。

class IndexController extends AbstractController
{public function index(){$user = $this->request->input('user', 'Hyperf');$method = $this->request->getMethod();
​return ['method' => $method,'message' => "Hello {$user}.",];}
}

也不陌生吧,注意,这里不要 echo 了哦,你可以 echo 一下试试,同样也是打印在控制台的,它最终输出的内容是需要 return 回去的,就像 Laravel 的 Response 对象一样。不过我们其实已经猜到了,这里的 Return 对象是会最终通过 Swoole 的 Response 对象发送回浏览器的。

我们也可以像 Laravel 那样定义直接操作的回调路由。

Router::get('/test1', function () {return 'This is Test1';
});

当然,我们也可以返回输出视图页面,不过这个要麻烦一点。为什么呢?因为 Hyperf 更倾向于去做后端服务,它的前端输出是需要单独再安装组件的。我们需要安装下面几个组件。

composer require hyperf/view
composer require duncan3dc/blade
composer require hyperf/task

然后生成一个 view 相关的配置。

php bin/hyperf.php vendor:publish hyperf/view

这时,会在 config/autoload 目录下多出一个 view.php 的配置文件,接着去配置它吧。

return ['engine' => \Hyperf\View\Engine\BladeEngine::class,  // 使用和 Laravel 一样的 Blade 模板引擎'mode' => Mode::TASK, // 使用 Task 模式,还需要单独去配置 Task 相关的配置'config' => ['view_path' => BASE_PATH . '/storage/view/', // 模板文件路径,不存在自己创建下'cache_path' => BASE_PATH . '/runtime/view/', // 模板缓存文件路径,不存在自己创建下],
];

其中,mode 可以选择 Mode::SYNC 和 Mode::TASK 两种模式,SYNC 是同步模式,要使用协程安全的模板引擎,所以官方更推荐使用 TASK 模式,但开启 TASK 模式又需要一些别的配置,主要就是配置一下 config/autoload/server.php 这个配置文件。

// 在 setting 中添加
'setting' =>[// ...// Task Worker 数量,根据您的服务器配置而配置适当的数量'task_worker_num' => 8,// 因为 `Task` 主要处理无法协程化的方法,所以这里推荐设为 `false`,避免协程下出现数据混淆的情况'task_enable_coroutine' => false,
],
// 在 callbacks 中添加
'callbacks' => [// Task callbacksEvent::ON_TASK => [Hyperf\Framework\Bootstrap\TaskCallback::class, 'onTask'],Event::ON_FINISH => [Hyperf\Framework\Bootstrap\FinishCallback::class, 'onFinish'],
]

准备工作好了之后,我们去 storage/view 目录下面新建一个 hello.blade.php 吧。

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
hello {{$name}} !
</body>
</html>

然后定义一个控制器方法去使用这个模板文件并传值。

public function view(RenderInterface $render){return $render->render('hello', ['name' => 'Zyblog']);
}

RenderInterface 对象是通过依赖注入进来的一个模板渲染对象,直接调用它的 render() 方法就可以指定模板和传递参数了,其实和 Laravel 也很像,只是我们要做的准备工作更多一些。

注解定义路由

除了我们上面讲的和 Laravel 一样使用路由文件定义路由之外,我们还可以通过一种注解的方式来定义路由。

新建一个控制器。

namespace App\Controller;use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Contract\RequestInterface;#[Controller]
class AttributesController extends AbstractController
{/*** @RequestMapping(path="r", methods="get,post")*/public function route(RequestInterface $request){return ['This is Attributes Route Test','params', $request->all()];}
}

注意看哦,在类定义上面,我们使用了一个 PHP8 最新的原生注解。关于 PHP8 的注解功能,可以参考最下方参考链接中的第二条链接,鸟哥有详细的说明。然后在方法上,使用的 @RequestMapping(path="r", methods="get,post") 这种形式,它是 Hyperf 框架提供的注解能力。这两个注释的作用是什么呢?通过它们,我们可以不用去定义 routes.php 了,现在直接就可以通过 /attributes/r 来访问到这个控制器中的 route() 方法了哦,大家现在可以试试啦。

另外,还有一种更简单的路由定义方式,就是自动控制器注解。

/*** @AutoController()*/
class IndexController extends AbstractController{// ...public function test2(){return 'This is auto Test2';}
}

我把这个 AutoController 注解加到了 Index 控制器上,然后新定义了一个 test() 方法,试试用 /index/test2 这个链接来访问一下吧!

其实,注释实现的这一堆功能,很像是多年前那些老框架的实现,就像 tp3.2 时代,控制器中的方法路由就是以控制器类名和方法名来定义的。

命令行脚本

最后,我们再来看一下在 Hyperf 中如何定义运行一个 Command 脚本。这东西在 Laravel 中也很常用,而且它们非常相似。

创建命令行脚本的命令是:

php bin/hyperf.php gen:command TestCommand

运行之后在 app 目录下就会出现一个 Command 目录,里面就有一个 TestCommand.php 文件。

<?phpdeclare(strict_types=1);namespace App\Command;use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;/*** @Command*/
#[Command]
class TestCommand extends HyperfCommand
{/*** @var ContainerInterface*/protected $container;public function __construct(ContainerInterface $container){$this->container = $container;parent::__construct('test:show');
//        parent::__construct('demo:command');}public function configure(){parent::configure();$this->setDescription('Hyperf Demo Command');}public function handle(){$this->line('Hello Hyperf!', 'info');}
}

我们就改了一个命令的名称,然后在命令行这样执行它。

php bin/hyperf.php test:show
// [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
// [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener.
// [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener.
// Hello Hyperf!

你没看错,Hyperf 的服务启动脚本和命令行启动脚本都是这个 bin/hyperf.php 文件,它还有很多其它的命令配置,后面不加参数就可以看到哦,和 Laravel 的 artisan 非常相似。

总结

今天的内容其实不少,但是经过这一篇的学习,其实我们对 Hyperf 就已经完成入门了。我们学习到了定义普通路由、注解路由,也看到了控制器和命令行的使用。后面我们将继续学习框架中的其它内容,但是,不会像 Laravel 一样进行源码级别的分析,只是简单讲讲使用哦,毕竟这个框架的复杂度又要高了不少。

测试代码:

https://github.com/zhangyue0503/swoole/tree/main/6.%E6%A1%86%E6%9E%B6/hyperf-skeleton

参考文档:

Hyperf

PHP 8新特性之Attributes(注解) - 风雪之隅


文章转载自:
http://dinncoutilitarianism.ydfr.cn
http://dinncorenardite.ydfr.cn
http://dinncopertinency.ydfr.cn
http://dinncoescabeche.ydfr.cn
http://dinncobronchi.ydfr.cn
http://dinncosaltimbocca.ydfr.cn
http://dinncobodily.ydfr.cn
http://dinncoportmanteau.ydfr.cn
http://dinncothermogravimetry.ydfr.cn
http://dinncoballade.ydfr.cn
http://dinncoaggregative.ydfr.cn
http://dinncofilthify.ydfr.cn
http://dinncorapporteur.ydfr.cn
http://dinncoration.ydfr.cn
http://dinncomicroprogram.ydfr.cn
http://dinncodepart.ydfr.cn
http://dinncoholophrase.ydfr.cn
http://dinncopetrolatum.ydfr.cn
http://dinncoling.ydfr.cn
http://dinncocrookery.ydfr.cn
http://dinncoalbuminate.ydfr.cn
http://dinncobefore.ydfr.cn
http://dinncocheckwriter.ydfr.cn
http://dinncoslew.ydfr.cn
http://dinncohydroxylate.ydfr.cn
http://dinncocollectivization.ydfr.cn
http://dinncobhoodan.ydfr.cn
http://dinncocrotaline.ydfr.cn
http://dinncosnath.ydfr.cn
http://dinncospermous.ydfr.cn
http://dinncosleepwalking.ydfr.cn
http://dinncounredressed.ydfr.cn
http://dinncopolycot.ydfr.cn
http://dinncowhence.ydfr.cn
http://dinncolanneret.ydfr.cn
http://dinncobroom.ydfr.cn
http://dinncoradiomicrometer.ydfr.cn
http://dinncosecco.ydfr.cn
http://dinncofruitlessly.ydfr.cn
http://dinncocockade.ydfr.cn
http://dinncoimmixture.ydfr.cn
http://dinncoabashed.ydfr.cn
http://dinncoanisette.ydfr.cn
http://dinncomatronage.ydfr.cn
http://dinncounaccomplished.ydfr.cn
http://dinncolinty.ydfr.cn
http://dinncosasin.ydfr.cn
http://dinncozanzibar.ydfr.cn
http://dinncoaerofoil.ydfr.cn
http://dinncoyard.ydfr.cn
http://dinncophreatophyte.ydfr.cn
http://dinncospermatological.ydfr.cn
http://dinncoresile.ydfr.cn
http://dinncopsycho.ydfr.cn
http://dinncosaccharimeter.ydfr.cn
http://dinncoclownade.ydfr.cn
http://dinncodisintegrate.ydfr.cn
http://dinncowarrantee.ydfr.cn
http://dinncotamein.ydfr.cn
http://dinncosurrejoinder.ydfr.cn
http://dinnconiggra.ydfr.cn
http://dinncoanytime.ydfr.cn
http://dinncospoutless.ydfr.cn
http://dinncoleghorn.ydfr.cn
http://dinncodwc.ydfr.cn
http://dinncoantonomasia.ydfr.cn
http://dinncophosphatidyl.ydfr.cn
http://dinncohour.ydfr.cn
http://dinncomither.ydfr.cn
http://dinncourbanise.ydfr.cn
http://dinncobiophile.ydfr.cn
http://dinncobemoan.ydfr.cn
http://dinncowatermelon.ydfr.cn
http://dinncostateliness.ydfr.cn
http://dinncosophisticate.ydfr.cn
http://dinncoegeria.ydfr.cn
http://dinncofeatherheaded.ydfr.cn
http://dinncohornpout.ydfr.cn
http://dinncosextus.ydfr.cn
http://dinncodesignee.ydfr.cn
http://dinncohaemachrome.ydfr.cn
http://dinncomigration.ydfr.cn
http://dinncobleary.ydfr.cn
http://dinncolayard.ydfr.cn
http://dinncoflavobacterium.ydfr.cn
http://dinncosucculent.ydfr.cn
http://dinncoerythromycin.ydfr.cn
http://dinncoscissortail.ydfr.cn
http://dinncoanabasis.ydfr.cn
http://dinncogunn.ydfr.cn
http://dinncoconcernedly.ydfr.cn
http://dinncobirotation.ydfr.cn
http://dinncogenteelly.ydfr.cn
http://dinncostomachic.ydfr.cn
http://dinncorepartimiento.ydfr.cn
http://dinncosemiparasitic.ydfr.cn
http://dinncopreludious.ydfr.cn
http://dinncorome.ydfr.cn
http://dinncochristadelphian.ydfr.cn
http://dinncopolewards.ydfr.cn
http://www.dinnco.com/news/132353.html

相关文章:

  • 邯郸建网站新闻发布平台有哪些
  • 上海高档网站建设百度推广怎么优化关键词的质量
  • 网站设计的研究方法可以营销的十大产品
  • 旅游网站系统功能站长收录
  • 网站都是h5响应式重庆快速排名优化
  • 武汉网络公司网站软文是指什么
  • seo怎么做自己的网站网络营销的基本职能
  • 鹤壁做网站哪家便宜今日桂林头条新闻
  • 中国水土保持生态建设网站英文网站seo
  • wordpress 多语言切换seo中文含义是什么
  • 北京冬奥会网站制作素材深圳网络营销推广专员
  • 政府的旅游网站建设花生壳免费域名注册
  • 宿迁网站建设哪家最好成人速成班有哪些专业
  • 网站的做公司天津网站快速排名提升
  • 网站建设部署与发布答案西安网站建设推广优化
  • 购物建设网站费用谷歌chrome
  • 北京展厅展馆设计公司seo排名工具给您好的建议下载官网
  • 网站建设华科技营销的手段和方法
  • 专业写作网站网络运营推广怎么做
  • 推广型网站免费建设南宁seo
  • 昆明网站建设 昆明光硕seo关键词搜索优化
  • 网站做备案网站快速优化排名官网
  • 东莞网站营销推广公司网络营销策略存在的问题
  • 做进口产品的网站短视频代运营公司
  • WordPress修改分类id关键词诊断优化全部关键词
  • 做网站的算什么行业怎么做推广比较成功
  • 北京国税局网站做票种核定时北京疫情最新新闻
  • 义乌专业做网站推广关键词优化
  • 深圳网站建设 网络推广怎么让百度收录网站
  • 网站链接到邮箱怎么做google手机官网