Virtual page with Rewirte Api without Custom Post Type

Regards @mmirus.

Thanks for the time and the help provided.

In add_rewrite_endpoint (‘virtual’, EP_PERMALINK) EP_PERMALINK did not work and I changed it to EP_ALL. Yes it worked.

After analyzing I did not need an endpoint, so I combined this code in filter.php:

add_filter('generate_rewrite_rules', function ($wp_rewrite) {
$wp_rewrite->rules = array_merge(
    ['virtual/?$' => 'index.php?virtual=1'],
    $wp_rewrite->rules
);

});
add_filter(‘query_vars’, function ($query_vars) {
$query_vars[] = ‘virtual’;
return $query_vars;
});

add_filter(‘the_posts’, function (array $posts, \WP_Query $query) {
// modify this to check for whatever will identify your virtual page request (i.e., your query var)
if (!isset($query->query_vars[‘virtual’])) {
return $posts;
}

// Returning a fake post bypasses WP's 404 logic
$title = 'Virtual Page';
$post = [
    'ID' => -100,
    'post_title' => $title,
    'post_name' => sanitize_title($title),
    'post_content' => '',
    'post_excerpt' => '',
    'post_parent' => 0,
    'menu_order' => 0,
    'post_type' => 'page',
    'post_status' => 'publish',
    'comment_status' => 'closed',
    'ping_status' => 'closed',
    'comment_count' => 0,
    'post_password' => '',
    'to_ping' => '',
    'pinged' => '',
    'guid' => home_url($query->getUrl()),
    'post_date' => current_time('mysql'),
    'post_date_gmt' => current_time('mysql', 1),
    'post_author' => 0,
    'is_virtual' => true,
    'filter' => 'raw',
];

return [
    new \WP_Post((object)$post),
];

}, 10, 2);

And in setup.php:

add_action('template_redirect', function () {
global $wp_query;

// bail if the virtual endpoint was not called
// modify this to check for whatever will identify your virtual page request (i.e., your query var)
if (!isset($wp_query->query_vars['virtual'])) {
    return;
}

$template = locate_template(["views/virtual.blade.php"]);

if ($template) {
    // customize the passed array if you need to send data to your template; omit if not
    echo template($template, [
        'title' => 'Virtual Page',
        'description' => 'This is how you might render a virtual page.',
    ]);
    die();
}

});

Aunge Controllers in sage work with the hierarchy of WordPress templates, is there any way a controller works with a virtual page just like they do in sage?

Thank you