dbeurive/slim-controller 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

dbeurive/slim-controller

Composer 安装命令:

composer require dbeurive/slim-controller

包简介

This package contains a controller manager for the SLIM framework

README 文档

README

Introduction

This package allows the use of "controllers" with the Slim framework.

Synopsis

Define the controllers

$ tree /path
/path
└── to
    └── controllers
        ├── ProfileController.php
        └── UserController.php

Please note the suffix used to create the controllers' file names: "Controller.php". This suffix can be changed.

<?php

namespace dbeurive\Slim\Test\controller0;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use dbeurive\Slim\controller\Controller;

/**
 * Class ProfileController
 * @package dbeurive\Slim\Test\controller0
 */
class ProfileController extends Controller
{
    /**
     * Create or update a profile.
     * @param Request $request
     * @param Response $response
     * @return Response
     */
    public function actionPostSet(Request $request, Response $response) {
        $response->getBody()->write("Profile has been set! (" . $this->app->getContainer()[FLAG] . ')');
        return $response;
    }

    /**
     * Get a profile.
     * @param Request $request
     * @param Response $response
     * @return Response
     * @uri-params {id}
     */
    public function actionGetGet(Request $request, Response $response) {
        $response->getBody()->write("This is the requested profile data (" . $this->app->getContainer()[FLAG] . ')');
        return $response;
    }
}

Please note the names of the methods that implement the controllers' actions ("actionPostSet" and "actionGetGet"). Actions' names start with the prefix "action", followed by the name of the expected HTTP method (see the Slim's documentation).

Please note the annotation "@uri-params {id}" (for the method "actionGetGet()"). This means that the URI associated with this action is "/profile/get/{id}".

Please have a look to the controller ProfileController.php and the controller UserController.php.

Create the index that lists all controllers

php slim-controller.php index --index-path /app/data/index.json /path/to/controllers/

The index "/app/data/index.json" contains data about the controllers. Please click here to see the content of this file.

Please click here to see the script slim-controller.php.

You can dump the content of the index in a human readable way:

$ php slim-controller.php dump --host http://www.slim-controller.localhost /app/data/index.json
POST   http://www.slim-controller.localhost/profile/set
       /path/to/controllers/ProfileController.php
       \dbeurive\Slim\Test\controller0\ProfileController::actionPostSet
GET    http://www.slim-controller.localhost/profile/get/{id}
       /path/to/controllers/ProfileController.php
       \dbeurive\Slim\Test\controller0\ProfileController::actionGetGet
POST   http://www.slim-controller.localhost/user/login
       /path/to/controllers/UserController.php
       \dbeurive\Slim\Test\controller0\UserController::actionPostLogin
GET    http://www.slim-controller.localhost/user/get/{id}
       /path/to/controllers/UserController.php
       \dbeurive\Slim\Test\controller0\UserController::actionGetGet

Implement the Slim application

use dbeurive\Slim\controller\Manager as ControllerManager;

$app = new \Slim\App([]);
ControllerManager::start($app, '/app/data/index.json');
$app->run();

Please note that, within this example, only the actions within the expected controller (depending on the URL) will be declared. For example, let's says that the URL is: http://www.slim-controller.localhost/profile/get/10. The expected controller is "ProfileController".

This behaviour can be modified. Indeed, it is possible to configure the controller manager so that it will systematically register all routes (from all controllers). To do that, we should have written: ControllerManager::start($app, '/app/data/index.json', true);

Please, click here to see the real example.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Creative
  • 更新时间: 2017-01-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固