定制 brtriver/slim3-controller 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

brtriver/slim3-controller

Composer 安装命令:

composer require brtriver/slim3-controller

包简介

Anonymous controller and trait class for Slim3

README 文档

README

Build Status

Anonymous controller and trait for Slim3 with PHP7. We can use anonymous classes in PHP7. So I use anonymous classes instead of anonymous functions. Slim3 now supports PSR-7 interfaces for its Request and Response objects. And Slim3 uses extends objects, and use it in your anonymous functions.

But we use Request and Response object directly, so I try to use anonymous classes to write controller action logic and it is to be readability. read more

Requirements

  • PHP 7.0 or later.
  • Slim3

Install

install via composer

composer require brtriver/slim3-controller

Usage

you can read example code.

sample code is blow:

use Brtriver\Controller\Controller;

$app->get('/hello/{name}', new class($app) extends Controller {

        public function action($args)
        {
            return $this->render($this->container['greet'] . $args['name']);
        }
});
  • Controller class has these methods and class properties.
    • methods:
      • action($args): you should write your controller logic in this method.
      • render($output): helper method to render the $output with 200 status
      • render404($output): helper method to render the $output with 404 status
    • properties:
      • $this->request
      • $this->response
      • $this->app
      • $this->container

If use Template engine (PHP or Twig etc...), a renderWithT method in Templatable trait is available:

use Brtriver\Controller\Controller;
use Brtriver\Controller\Templatable;
$app->get('/hello/{name}', new class($app) extends Controller {
        use Templatable;

        public function action($args)
        {
            return $this->renderWithT('web.html', ['name' => $args['name']]);
        }
});

If use JSON response, a renderWithJson method in JsonResponse trait is available:

use Brtriver\Controller\Controller;
use Brtriver\Controller\JsonResponse;
$app->get('/json/{name}', new class($app) extends Controller {
	    use JsonResponse;

        public function action($args)
        {
            return $this->renderWithJson(['name' => $args['name']]);
        }
});

License

slim3-controller is licensed under the MIT license.

统计信息

  • 总下载量: 22
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-01-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固