Category archive page routing
Diff
src/Routing/CategoryRoute.php | 56 +++++++++++++++++++++++++++++++++++-
src/Routing/Routing.php | 31 +++++++++++++++++++-
src/Routing/RoutingServiceProvider.php | 1 +-
src/Support/Wordpress.php | 11 +++++++-
4 files changed, 99 insertions(+)
@@ -0,0 +1,56 @@
<?php
namespace Koselig\Routing;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Koselig\Support\Wordpress;
/**
* Category page route, used when Wordpress reports
* this page is a category page.
*
* @author Jordan Doyle <jordan@doyle.wf>
*/
class CategoryRoute extends Route
{
/**
* Post types for this archive route to hook onto.
*
* @var array
*/
private $categories;
/**
* Create a new Route instance.
*
* @param array|string $methods
* @param array $postTypes
* @param \Closure|array $action
*
* @return void
*/
public function __construct($methods, $postTypes, $action)
{
parent::__construct($methods, $postTypes, $action);
$this->categories = $this->uri;
$this->uri = 'category/' . (implode('/', $this->categories) ?: 'all');
}
/**
* Determine if the route matches given request.
*
* @param \Illuminate\Http\Request $request
* @param bool $includingMethod
*
* @return bool
*/
public function matches(Request $request, $includingMethod = true)
{
if (!empty($this->getAction()['domain']) && !Wordpress::multisite($this->getAction()['domain'])) {
return false;
}
return Wordpress::category($this->categories);
}
}
@@ -28,6 +28,37 @@ class Routing
}
/**
* Register a new category route with the router. Optionally supply
* the categories you'd like to supply with this route.
*
* @param callable|string|array $categories
* @param callable|array|string|null $action
*
* @return \Illuminate\Routing\Route
*/
public function category($categories = [], $action = [])
{
if (empty($action)) {
$action = $categories;
$categories = [];
}
if (!is_array($categories)) {
$categories = [$categories];
}
$action = $this->formatAction($action);
$route = (new CategoryRoute($action['method'], $categories, $action))
->setRouter(app('router'))
->setContainer(app(Container::class));
$route = $this->applyStack($route);
return Route::getRoutes()->add($route);
}
/**
* Register a new page route with the router.
*
* @param string $slug slug to match
@@ -29,5 +29,6 @@ class RoutingServiceProvider extends ServiceProvider
Router::macro('archive', [$routing, 'archive']);
Router::macro('singular', [$routing, 'singular']);
Router::macro('author', [$routing, 'author']);
Router::macro('category', [$routing, 'category']);
}
}
@@ -35,6 +35,17 @@ class Wordpress
}
/**
* Check if the current page is a category page.
*
* @param array|string $categories categories to test for
* @return bool
*/
public static function category($categories = '')
{
return query()->category($categories);
}
/**
* Check if the current page is an archive page.
*
* @param string|array|null $types check if the archive page is for this type