atk14 / sluggish-router
Base class for ATK14 routers generating nice looking URLs with slugs
Installs: 28 406
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 0
Open Issues: 0
Requires
- php: >=5.3.0
Requires (Dev)
- atk14/dictionary: *
- atk14/string4: *
- atk14/tester: *
- mockery/mockery: 0.9
This package is auto-updated.
Last update: 2024-10-27 15:08:16 UTC
README
Base class for ATK14 routers generating nice looking URLs with slugs
It can handle "detail" URLs for models that have method getSlug($lang)
and static method GetInstanceBySlug($slug,&$lang)
. You can find both in the Atk14Catalog.
Basic usage
Router class:
<?php
// file: config/routers/articles_router.php
class ArticlesRouter extends SluggishRouter {
var $patterns = [
"en" => ["index" => "/articles/", "detail" => "/articles/<slug>/"],
"cs" => ["index" => "/clanky/", "detail" => "/clanky/<slug>/"],
"sk" => ["index" => "/sk/clanky/", "detail" => "/sk/clanky/<slug>/"],
];
// var $model_class_name = "Article"; // by default determined automatically according to the router's class name
// var $target_controller_name = "articles"; // by default determined automatically according to the router's class name
}
Loading router:
<?php
// file: config/routers/load.php
Atk14Url::AddRouter("ArticlesRouter"); // the default namespace ("")
Atk14Url::AddRouter("blog","ArticlesRouter"); // loading ArticlesRouter also into namespace blog
In a template:
{a controller="articles" action="detail" id=123}Here is the article{/a}<br>
{a controller="articles" action="index"}Show all articles{/a}
Rendered HTML:
<a href="/articles/why-is-the-atk14-so-cool/">Here is the article</a><br>
<a href="/articles/">Show all articles</a>
An easier way to set the routes:
<?php
// file: config/routers/articles_router.php
class ArticlesRouter extends SluggishRouter {
var $patterns = [
"en" => "articles", // same as ["index" => "/articles/", "detail" => "/articles/<slug>/"]
"cs" => "clanky", // same as ["index" => "/clanky/", "detail" => "/clanky/<slug>/"]
"sk" => "sk/clanky", // same as ["index" => "/sk/clanky/", "detail" => "/sk/clanky/<slug>/"]
];
}
Setting only detail routes:
<?php
// file: config/routers/articles_router.php
class ArticlesRouter extends SluggishRouter {
var $patterns = [
"en" => "/articles/<slug>/", // same as ["detail" => "/articles/<slug>/"]
"cs" => "/clanky/<slug>/", // same as ["detail" => "/clanky/<slug>/"]
"sk" => "/sk/clanky/<slug>/", // same as ["detail" => "/sk/clanky/<slug>/"]
];
}
Fallback route
A fallback route for all the unlisted languages can be specified using the asterisk symbol.
<?php
// file: config/routers/articles_router.php
class ArticlesRouter extends SluggishRouter {
var $patterns = [
"*" => ["index" => "/<lang>/articles/", "detail" => "/<lang>/articles/<slug>/"],
];
}
Installation
Use the Composer to install SluggishRouter.
cd path/to/your/project/
composer require atk14/sluggish-router
Testing
composer update --dev
cd test
../vendor/bin/run_unit_tests
Licence
SluggishRouter is free software distributed under the terms of the MIT license