metabolism / wordpress-bundle

Use Wordpress and Symfony together using a Symfony bundle
MIT License
59 stars 17 forks source link

Modifying routing / exposing Permastruct class #37

Open markeasting opened 11 months ago

markeasting commented 11 months ago

Hi again,

Backstory: I was trying to modify my BlogController, spliting up our post type actions into multiple controllers (otherwise, the controllers tend to become quite large, having many different post types). Naturally, I tried adding new routes to my routing config, but these weren't properly integrated with the Wordpress rewrite system.

I saw that the Permastruct class in config/routing.php parses the Wordpress rewrites into Symfony routes (nice!). However, since the class is not located in the /src directory, I cannot autoload this in my own routing.php file... Maybe move it to src/Routing and import it into the config file?

Also, it would be great if we could configure which controllers handle certain (custom) content types. Here's a proposal:

new Permastruct($collection, '', [
    'custom-post_type' => CustomController,
    'post' => PostController,
    'page' => PageController,
    'default' => BlogController,
]);

Besides this, it would help to type-hint controller methods using some kind of bundle-exposed interface:

namespace Metabolism\WordpressBundle\Interface;

interface ControllerInterface {
    function postAction(Post $post);
    function archiveAction(array $posts, PaginationService $paginationService);
}

// Alternative: 
interface ControllerInterface {
    function show(Post $post);
    function index(array $posts, PaginationService $paginationService); // or achive() / list()
}

Maybe it would be even better if these method names could be specified by the user (like [CustomController::class, 'myOwnMethod'], giving a bit more more flexibility.

Would love to hear your thoughts!

jerome-barbato commented 11 months ago

Hi @markeasting that's an interesting request ! I think I can update the routing.php to allow multiple controllers but giving the ability to choose method name seems to be tricky, I will try few things and keep you up to date !

markeasting commented 11 months ago

Hi Jerome,

I've been trying a few things, here's a system that I got working:

new Permastruct($collection, '', [
    'default' => 'BlogController', // Same as the original '$controllername' - now used as a fallback
    'home' => [GenericBlogController::class, 'home'],
    'guide' => [GuideController::class, 'single'],
    'guide_archive' => [GuideController::class, 'archive'],
]);

In Permastruct::getControllerName(), the given mapping is used to generate the routes.

I think this would work - the only improvement is that I'd like to use the 'base' name, to be assigned to a single controller (e.g. everything for guide in a single controller, with generated methods for post/archive/paged). So maybe just pass GuideController::class (without method) and let the bundle generate method names for it. Should have some kind of consistent interface or base controller though.

Let me know what you think. For now I'll keep working on it and create a pull request for it at some point.

PS. Maybe it's also possible to 'reverse' the routing, e.g. generating routes on the Symfony side and submitting that to $wp_rewrite, giving full control over Wordpress.