chomenko/inline-routing

此软件包最新版本(dev-master)没有提供许可证信息。

Nette 框架的内部路由

dev-master 2019-09-23 11:03 UTC

This package is auto-updated.

Last update: 2024-09-23 22:17:41 UTC


README

Symfony 路由 适配到 Nette

要求

使用 Composer 安装以下软件包。软件包 kdyby/doctrine 和 kdyby/annotations 需要配置。

安装和配置

使用 Composer 是安装 chomenko/inline-routing 的最佳方式

composer require chomenko/inline-routing

并现在使用您的 neon 配置启用扩展

extensions:
	console: Kdyby\Console\DI\ConsoleExtension
	events: Kdyby\Events\DI\EventsExtension
	annotations: Kdyby\Annotations\DI\AnnotationsExtension
	doctrine: Kdyby\Doctrine\DI\OrmExtension
	
	inlineRouting: Chomenko\InlineRouting\DI\InlineRoutingExtension

并将 trait 添加到基本展示者中

<?php

namespace App;

use Chomenko\InlineRouting\InlineRouting;
use Nette\Application\UI\Presenter;

abstract class BasePresenter extends Presenter
{
	use InlineRouting;
}

用法

创建简单路由。您可以通过使用注解 @Inline\Route 来创建路由
必须为展示者创建基本的 Nette 路由。

<?php

namespace App;

use Chomenko\InlineRouting\Inline;

class SimplePresenter extends BasePresenter
{
	
	/**
	 * @link http://example.com/helow-world
	 *
	 * @Inline\Route("/hello-world", name="first-route")
	 */
	public function helloWorld()
	{
		$this->payload->hello = "world";
		$this->sendPayload();
	}
	
	/**
	 * @link http://example.com/hello-parameters/this-si-foo/prefix-this-is-bar
	 * 
	 * @Inline\Route("/hello-parameters/{foo}/prefix-{bar}", name="hello-parameters")
	 *
 	 * @param mixed $foo
 	 * @param mixed $bar
	 */
	public function helloParameters($foo, $bar)
	{
		$this->payload->foo = $foo;
		$this->payload->bar = $bar;
		$this->sendPayload();
	}
	
}

您也可以在展示者上创建路由

<?php

namespace App;

use Chomenko\InlineRouting\Inline;

/**
 * @Inline\Route("/prefix", name="prefix_")
 */
class SimplePresenter extends BasePresenter
{
	
	/**
	 * @link http://example.com/prefix/hello-world
   	 * 
	 * @Inline\Route("/hello-world", name="first-route")
	 */
	public function helloWorld()
	{
		$this->payload->hello = "world";
		$this->sendPayload();
	}
	
}

您也可以将值转换成实体

<?php

namespace App;

use Chomenko\InlineRouting\Inline;
use Entity\User;

/**
 * @Inline\Route("/user", name="users-")
 */
class SimplePresenter extends BasePresenter
{
	
	/**
	 * @link http://example.com/user/detail/1
   	 * 
	 * @Inline\Route("/detail/{userId}", name="detail")
 	 * @Inline\EntityTransform(
	 *     class="Entity\User",
	 *     parameter="userId"
	 * ) 
	 * 
 	 * @param User $user
	 */
	public function detail(User $user)
	{
		$this->payload->userName = $user->getName();
		$this->sendPayload();
	}
	
}