ZenstruckRedirectBundle
This bundle adds entities for redirects and 404 errors.
For redirects, 404 errors are intercepted and the requested path is looked up. If a match is found it redirects to the found redirect's destination. The count and last accessed date are updated as well. A redirect form type and validation is available as well.
404 errors can be logged as well. Each 404 error is it's own record in the database. The path, full URL, timestamp, and referer are stored. Storing each error as a separate record allows viewing statistics over time and seeing all the referer URLs. When a redirect is created or updated, 404 records that match it's path are deleted.
Installation
Install with composer:
$ composer require zenstruck/redirect-bundle
Enable the bundle: This Step is only needed if you are not using Symfony Flex.
// config/bundles.php return [ // ... ZenstruckRedirectBundleZenstruckRedirectBundle::class => ['all' => true], ];
Configuration
NOTE: A NotFound
or Redirect
or both must be configured.
Redirect
Create your redirect class inheriting the MappedSuperClass this bundle provides:
namespace AppEntity; use ZenstruckRedirectBundleModelRedirect as BaseRedirect; use DoctrineORMMapping as ORM; /** * @ORMEntity * @ORMTable(name="redirects") */ class Redirect extends BaseRedirect { /** * @ORMId * @ORMColumn(type="integer") * @ORMGeneratedValue(strategy="AUTO") */ private $id; }
Set this class in your zenstruck_redirect.yml
:
zenstruck_redirect: redirect_class: AppEntityRedirect
Update your schema (or use a migration):
$ bin/console doctrine:schema:update --force
NotFound
Create your not found class inheriting the MappedSuperClass this bundle provides:
namespace AppEntity; use ZenstruckRedirectBundleModelNotFound as BaseNotFound; use DoctrineORMMapping as ORM; /** * @ORMEntity * @ORMTable(name="not_founds") */ class NotFound extends BaseNotFound { /** * @ORMId * @ORMColumn(type="integer") * @ORMGeneratedValue(strategy="AUTO") */ private $id; }
Set this class in your zenstruck_redirect.yml
:
zenstruck_redirect: not_found_class: AppEntityNotFound
Update your schema (or use a migration):
$ bin/console doctrine:schema:update --force
Form Type
This bundle provides a form type (zenstruck_redirect
) for creating/editing redirects.
$redirect = // ... $form = $this->createForm('zenstruck_redirect', $redirect);
You may want to disable the source
field for already created redirects:
// new action $redirect = new Redirect(); $form = $this->createForm('zenstruck_redirect', $redirect); // edit action $redirect = // get from database $form = $this->createForm('zenstruck_redirect', $redirect, array('disable_source' => true));
Full Default Configuration
zenstruck_redirect: redirect_class: ~ # Required if not_found_class is not set not_found_class: ~ # Required if redirect_class is not set model_manager_name: ~ # When enabled, when a redirect is updated or created, the NotFound entities with a matching path are removed. remove_not_founds: true
版权声明:
1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。2、网站不提供资料下载,如需下载请到原作者页面进行下载。
3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考学习用!
4、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。