Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifying routing / exposing Permastruct class #37

Open
markeasting opened this issue Oct 9, 2023 · 2 comments
Open

Modifying routing / exposing Permastruct class #37

markeasting opened this issue Oct 9, 2023 · 2 comments

Comments

@markeasting
Copy link

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
Copy link
Member

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants