diff --git a/README.md b/README.md index b68300fd..5db0641a 100644 --- a/README.md +++ b/README.md @@ -335,6 +335,53 @@ You can then use the `Slugify::slugify()` method in your controllers: $url = Slugify::slugify('welcome to the homepage'); ``` +### Laminas + +Slugify can be easely used in Laminas applications. Included bridge provides a service and a view helper +already registered for you. + +Just enable the module in your configuration like this. + +```php +return array( + //... + + 'modules' => array( + 'Application', + 'Cocur\Slugify\Bridge\Laminas' // <- Add this line + //... + ) + + //... +); +``` + +After that you can retrieve the `Cocur\Slugify\Slugify` service (or the `slugify` alias) and generate a slug. + +```php +/** @var \Laminas\ServiceManager\ServiceManager $sm */ +$slugify = $sm->get('Cocur\Slugify\Slugify'); +$slug = $slugify->slugify('Hällo Wörld'); +$anotherSlug = $slugify->slugify('Hällo Wörld', '_'); +``` + +In your view templates use the `slugify` helper to generate slugs. + +```php +slugify('Hällo Wörld') ?> +slugify('Hällo Wörld', '_') ?> +``` + +The service (which is also used in the view helper) can be customized by defining this configuration key. + +```php +return array( + 'cocur_slugify' => array( + 'reg_exp' => '/([^a-zA-Z0-9]|-)+/' + ) +); +``` + ### Zend Framework 2 Slugify can be easely used in Zend Framework 2 applications. Included bridge provides a service and a view helper diff --git a/composer.json b/composer.json index 8913fc9d..dc466d42 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,9 @@ "twig/twig": "<2.12.1" }, "require-dev": { + "laminas/laminas-modulemanager": "~2.8", + "laminas/laminas-servicemanager": "~3.3", + "laminas/laminas-view": "~2.9", "laravel/framework": "~5.1", "latte/latte": "~2.2", "league/container": "^2.2.0", diff --git a/src/Bridge/Laminas/Module.php b/src/Bridge/Laminas/Module.php new file mode 100644 index 00000000..0eaf7646 --- /dev/null +++ b/src/Bridge/Laminas/Module.php @@ -0,0 +1,55 @@ +> + */ + public function getServiceConfig() + { + return [ + 'factories' => [ + Slugify::class => SlugifyService::class, + ], + 'aliases' => [ + 'slugify' => Slugify::class, + ] + ]; + } + + /** + * Expected to return \Laminas\ServiceManager\Config object or array to + * seed such an object. + * + * @return array>|Config + */ + public function getViewHelperConfig() + { + return [ + 'aliases' => [ + 'slugify' => SlugifyViewHelper::class + ], + 'factories' => [ + SlugifyViewHelper::class => SlugifyViewHelperFactory::class + ] + ]; + } +} diff --git a/src/Bridge/Laminas/SlugifyService.php b/src/Bridge/Laminas/SlugifyService.php new file mode 100644 index 00000000..16d4e7a5 --- /dev/null +++ b/src/Bridge/Laminas/SlugifyService.php @@ -0,0 +1,34 @@ +get('Config'); + + $slugifyOptions = isset($config[Module::CONFIG_KEY]['options']) ? $config[Module::CONFIG_KEY]['options'] : []; + $provider = isset($config[Module::CONFIG_KEY]['provider']) ? $config[Module::CONFIG_KEY]['provider'] : null; + + return new Slugify($slugifyOptions, $provider); + } + +} diff --git a/src/Bridge/Laminas/SlugifyViewHelper.php b/src/Bridge/Laminas/SlugifyViewHelper.php new file mode 100644 index 00000000..d6ddff93 --- /dev/null +++ b/src/Bridge/Laminas/SlugifyViewHelper.php @@ -0,0 +1,41 @@ +slugify = $slugify; + } + + /** + * @param string $string + * @param string|null $separator + * + * @return string + */ + public function __invoke($string, $separator = null) + { + return $this->slugify->slugify($string, $separator); + } +} diff --git a/src/Bridge/Laminas/SlugifyViewHelperFactory.php b/src/Bridge/Laminas/SlugifyViewHelperFactory.php new file mode 100644 index 00000000..b3d9b1d3 --- /dev/null +++ b/src/Bridge/Laminas/SlugifyViewHelperFactory.php @@ -0,0 +1,30 @@ +get('Cocur\Slugify\Slugify'); + return new SlugifyViewHelper($slugify); + } + +}