slimcontroller/slimcontroller

此包已被弃用且不再维护。未建议替代包。

Slim 框架的控制器扩展

0.4.3 2015-04-07 21:15 UTC

This package is not auto-updated.

Last update: 2022-10-01 03:40:48 UTC


README

SlimController 是 Slim 框架的扩展,提供 MVC 的 C。

仅使用 Slim,您可以非常快速地创建出色的应用程序。有时事情会失控,您只需要更多一点结构 - 至少我是这样。这就是 SlimController 的用途。

Latest Stable Version Total Downloads

Build Status

通过 composer 安装

创建一个 composer.json 文件

{
    "require": {
        "slimcontroller/slimcontroller": "0.4.3"
    },
    "autoload": {
        "psr-0": {
            "MyApp": "src/"
        }
    }
}

运行安装

composer.phar install --dev

快速入门

如果您了解 Slim 的工作原理,使用 SlimController 应该不会是件大事。

示例结构

为您的控制器和模板设置一个结构(仅供参考,按您的喜好进行操作)

mkdir -p src/MyApp/Controller templates/home

控制器

src/MyApp/Controller/Home.php 中创建您的第一个控制器

<?php

namespace MyApp\Controller;

class Home extends \SlimController\SlimController
{

    public function indexAction()
    {
        $this->render('home/index', array(
            'someVar' => date('c')
        ));
    }

    public function helloAction($name)
    {
        $this->render('home/hello', array(
            'name' => $name
        ));
    }
}

模板

以下是相应的两个示例模板

templates/home/index.php

This is the SlimController extension @ <?= $someVar ?>

templates/home/hello.php

Hello <?= $name ?>

Bootstrap index.php

此示例的最小化 Bootstrap 文件

<?php

// define a working directory
define('APP_PATH', dirname(__DIR__)); // PHP v5.3+

// load
require APP_PATH . '/vendor/autoload.php';

// init app
$app = New \SlimController\Slim(array(
    'templates.path'             => APP_PATH . '/templates',
    'controller.class_prefix'    => '\\MyApp\\Controller',
    'controller.method_suffix'   => 'Action',
    'controller.template_suffix' => 'php',
));

$app->addRoutes(array(
    '/'            => 'Home:index',
    '/hello/:name' => 'Home:hello',
));

$app->run();

运行

php -S localhost:8080

控制器

配置

controller.class_prefix

控制器类的可选前缀。将添加到路由之前。

使用 \\MyApp\\Controller 作为前缀,并给出路由

$app->addRoutes(array(
    '/'            => 'Home:index',
    '/hello/:name' => 'Home:hello',
));

转换为

$app->addRoutes(array(
    '/'            => '\\MyApp\\Controller\\Home:index',
    '/hello/:name' => '\\MyApp\\Controller\\Home:hello',
));

controller.class_suffix

控制器类的可选后缀。将添加到路由之后。

使用 Controller 作为后缀,并给出路由

$app->addRoutes(array(
    '/'            => 'Home:index',
    '/hello/:name' => 'Home:hello',
));

转换为

$app->addRoutes(array(
    '/'            => 'HomeController:index',
    '/hello/:name' => 'HomeController:hello',
));

controller.method_suffix

可选的方法后缀。添加到路由之后。

使用 Action 作为后缀,并给出路由

$app->addRoutes(array(
    '/'            => 'Home:index',
    '/hello/:name' => 'Home:hello',
));

转换为

$app->addRoutes(array(
    '/'            => 'Home:indexAction',
    '/hello/:name' => 'Home:helloAction',
));

controller.template_suffix

默认为 twig。将在 render() 方法中给出的模板名称之后添加。

扩展示例

路由

// how to integrate the Slim middleware
$app->addRoutes(array(
    '/' => array('Home:index', function() {
            error_log("MIDDLEWARE FOR SINGLE ROUTE");
        },
        function() {
            error_log("ADDITIONAL MIDDLEWARE FOR SINGLE ROUTE");
        }
    ),
    '/hello/:name' => array('post' => array('Home:hello', function() {
            error_log("THIS ROUTE IS ONLY POST");
        }
    ))
), function() {
    error_log("APPENDED MIDDLEWARE FOR ALL ROUTES");
});

控制器

<?php

namespace MyApp\Controller;

class Sample extends \SlimController\SlimController
{

    public function indexAction()
    {

        /**
         * Access \SlimController\Slim $app
         */

        $this->app->response()->status(404);


        /**
         * Params
         */

        // reads "?data[foo]=some+value"
        $foo = $this->param('foo');

        // reads "data[bar][name]=some+value" only if POST!
        $bar = $this->param('bar.name', 'post');

        // all params of bar ("object attributes")
        //  "?data[bar][name]=me&data[bar][mood]=happy" only if POST!
        $bar = $this->param('bar');
        //error_log($bar['name']. ' is '. $bar['mood']);

        // reads multiple params in array
        $params = $this->params(array('foo', 'bar.name1', 'bar.name1'));
        //error_log($params['bar.name1']);

        // reads multiple params only if they are POST
        $params = $this->params(array('foo', 'bar.name1', 'bar.name1'), 'post');

        // reads multiple params only if they are POST and all are given!
        $params = $this->params(array('foo', 'bar.name1', 'bar.name1'), 'post', true);
        if (!$params) {
            error_log("Not all params given.. maybe some. Don't care");
        }

        // reads multiple params only if they are POST and replaces non given with defaults!
        $params = $this->params(array('foo', 'bar.name1', 'bar.name1'), 'post', array(
            'foo' => 'Some Default'
        ));


        /**
         * Redirect shortcut
         */

        if (false) {
            $this->redirect('/somewhere');
        }


        /**
         * Rendering
         */

        $this->render('folder/file', array(
            'foo' => 'bar'
        ));

    }
}