biozshock / short-url-bundle
提供获取类似 http://your.host/~short 的短链接的服务和 twig 扩展
dev-master
2013-01-30 08:39 UTC
Requires
- php: >=5.3.2
- doctrine/doctrine-bundle: 1.1.*
- doctrine/orm: >=2.2.3,<2.4-dev
- symfony/symfony: >=2.0,<2.3-dev
This package is not auto-updated.
Last update: 2024-09-22 03:57:41 UTC
README
此包提供获取类似 http://your.host/~short 的短链接的服务和 twig 扩展
安装
将此包添加到您的项目中
使用 vendors 脚本
在您的 deps 文件中添加以下行
[BumzShortUrlBundle]
git=git://github.com/biozshock/ShortUrlBundle.git
target=bundles/Bumz/ShortUrlBundle
运行 vendors 脚本
$ php bin/vendors install
使用 Git 子模块
$ git submodule add git://github.com/biozshock/ShortUrlBundle.git vendor/bundles/Bumz/ShortUrlBundle
将 Bumz 命名空间添加到您的自动加载器中
<?php // app/autoload.php $loader->registerNamespaces(array( 'Bumz' => __DIR__.'/../vendor/bundles', // your other namespaces ));
将此包添加到应用程序的内核中
<?php // app/AppKernel.php public function registerBundles() { return array( // ... new Bumz\ShortUrlBundle\BumzShortUrlBundle(), // ... ); }
添加包的路由
# /app/config/routing.yml BumzShortUrlBundle: resource: "@BumzShortUrlBundle/Resources/config/routing.yml" prefix: /
示例
控制器中的短链接
<?php use Symfony\Bundle\FrameworkBundle\Controller\Controller; class UsersController extends Controller { public function getUserProfileShortAction() { ... $longUrl = $this->get('bumz_short_url.shortener')->shorten('http://example.com'); // $longUrl = '/~ShE' ... } }
在控制器中获取长链接
<?php use Symfony\Bundle\FrameworkBundle\Controller\Controller; class UsersController extends Controller { public function getUserProfileShortAction() { ... $shortUrl = 'aUty'; $longUrl = $this->get('bumz_short_url.shortener')->getLong($shortUrl); // $longUrl = 'http://example.com' ... } }
在 twig 模板中获取短链接
{{ 'http://example.com' | shortenUrl }} {# this will output something like /~ShE #}