alanmastro / short-url-bundle
提供获取类似http://your.host/~short的短链接的服务和twig扩展
1.0.1
2020-02-25 16:14 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 #}