nicklasos/router

此包最新版本(dev-master)没有可用的许可证信息。

类似Sinatra的路由器

dev-master 2014-08-22 12:17 UTC

This package is auto-updated.

Last update: 2024-09-25 07:15:38 UTC


README

#用法

##通过composer安装

{
    "require": {
        "nicklasos/router": "dev-master"
    }
}

##路由器

require 'vendor/autoload.php';

use Nicklasos\Router\App,
    Nicklasos\Router\View;

$app = new App;

$view = new View;
$view->setViewsPath(__DIR__ . '/views');
$view->setLayout('layout');

$app->get('/', function () {
    return 'home ';
});

$app->get('test/views', function () use ($view) {
    return $view->render('index', [
        'viewName' => 'This is index.php view file',
        'title' => 'Layout'
    ]);
});

$app->get('user/:id', function () {
    return $_GET['id'];
});

$app->get('test/:param/view/:test', function () {
    return $_GET['param'] . $_GET['test'];
});

$app->get('test', function () {
    return 'test';
});

$app->get('user/profile', function () {
    return 'user/profile';
});

$app->get('test/1/2', function () {
    return 'test/1/2';
});

$app->notFound(function () {
    return 'Not found';
});

$app->run();

##模板

<!-- layout.php -->
<h3><?= $title ?></h3>
<?= $this->render($view, $data) ?>

<!-- index.php -->
<p><?= viewName ?></p>