ayrel / seo-bundle
Symfony SEO 扩展包:使用请求属性创建 title 和 meta HTML 标签
dev-master
2017-01-27 08:56 UTC
Requires (Dev)
- phpunit/phpunit: 5.6.*
- satooshi/php-coveralls: ^1.0
- symfony/phpunit-bridge: ^3.1
This package is not auto-updated.
Last update: 2024-09-28 19:58:18 UTC
README
使用此包,您可以在配置文件中配置 HTML meta 标签和 title 标签:/app/config/seo.yml,或者通过添加 seo 注解。
您可以使用 Twig 模式编写 meta 标签。可用的变量是请求属性中的那些。
安装
### 步骤 1:下载
使用 composer...
composer require ayrel/seo-bundle dev-master
### 步骤 2:启用包 将包添加到您的应用程序内核
// app/AppKernel.php public function registerBundles() { return array( // ... new Ayrel\SeoBundle\AyrelSeoBundle(), // ... ); }
完成...现在您可以享受了!
配置您的第一个路由
use Ayrel\SeoBundle\Annotation\SeoAnnotation as Seo; class DefaultController extends Controller { /** * @Route("/", name="homepage") * @Seo({ * "title": "my Website | {{_route}}", * "description": "this is the route {{_route}} of my website" * }) */ public function indexAction(Request $request) { // replace this example code with whatever you need return $this->render('default/index.html.twig', [ 'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR, ]); } /*....*/ }
与 ParamConverter 组件一起使用
ParamConverter 组件将您的控制器的参数属性注册到请求属性中。
使用此包,您可以访问这些对象来编写惊人的模板。
use Ayrel\SeoBundle\Annotation\SeoAnnotation as Seo; use AppBundle\Entity\Product; class DefaultController extends Controller { /** * @Route("/product/{id}", name="product") * @Seo({ * "title": "{{product.title}} : {{product.price}} €", * "description": "{{product.desc}}" * }) */ public function productAction(Product $product) { /*.....*/ } /*....*/ }
在单个文件中配置所有路由
您可以通过注解设置您的 meta 标签模板。但您也可以编写一个 yml 文件来配置所有路由。
只需创建一个 app/config/seo.yml 文件
并配置您的模板
homepage: title: 'my beautifull title' description: 'this is a description' og_title: 'you can share this' og_description: 'this is a so interesting webpage....' product: title: '{{product.title}} : {{product.price}} €' description: '{{product.desc}}'
配置默认 meta
您可以为 meta 添加默认值。
只需编写您 app/config/config.yml 文件
ayrel_seo: default: title: 'my webisite | {_route}'
如果您想使用默认的 meta 值,请使用 NULL 值配置路由 meta。
homepage: title: ~
## 资源 Twig 策略