adspray / short-url-bundle
提供获取类似 http://your.host/~short 的短网址的服务和 twig 扩展
1.0.3
2021-02-10 13:57 UTC
Requires
- php: >=5.3.2
- doctrine/doctrine-bundle: ^1.6
- doctrine/orm: ^2.5
- symfony/framework-bundle: ~3.0|^4.0
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 #}