How to add a route for a PHTML page in theme?

I think one way to solve this is to “add new page” in a site and then use something like Blocks Plus to achieve the desired result.

But at least for this particular use case, I want to inquire about a more direct way.

Inside the theme (/themes/theme/view/omeka/site/item, there is a search.phtml page. It is more or less a shell that loads the “partial” (common/advanced-search).

I have a need to create a different search form (probably several actually), and I already have a partial (common/advanced-search/properties-codex) that exists.

I was hoping I could just copy search.phtml to a new file (e.g. codex.phtml), modify that shell to point to the new partial, and be able to call it as a URL (/item/codex rather than /item/search). Of course, it generates a route error (“Not found” - “Reason: error-controller-cannot-dispatch”) because the Omeka-S application doesn’t know about it yet.

When I look inside /application/config/routes.config.php, I see lots of stuff regarding controllers and routes. I am not a master of this yet, but have learned enough over the past 3 years. But what I do not find when I search in the text editor is anything for “item” or for “search”. It appears most of the other routes (what you would see/call in the URL) are there, but not these - so I’m guessing they are somewhere more “core” :slight_smile: Or are handled in some other way.

Hoping there is a quick hack / direct way to enable Omeka to recognize that when I type /item/codex, I want it to load /themes/theme/view/omeka/site/item/codex.phtml.

Basically, you can’t add URLs with a theme in Omeka.

The URLs available are set by routes and controllers, and those are handled by the core and by modules, so if you wanted to add a new URL a module would be the way to go. All the routes for Omeka S’s core are in that routes.config.php file, if you’re not seeing a particular one you’re looking for, it’s likely because the string you’re searching for is actually a variable (like “item” is a variable in a more generic “resource” route that’s shared between items, sets, etc.). It’s possible to just hack new routes in there directly (though you’d still need a controller anyway to add a new one), but a module is going to be a much more sustainable solution.

1 Like

OK got it. Controllers are still a barrier for me.

But here’s my clever solution that does not involve hacking core :slight_smile: Hopefully it will help some others - and I wonder if this could be translated into a simple “official” solution?

I created a placeholder page in Sites > Pages. I am also using CleanURL, so /page gets removed from the URL.

In my theme specifically - I’m not sure if every theme has this file - there is a layout.phtml file located here:

I also already have a “partial” file (properties-codex.phtml) created here:

A partial file is really just a block of raw HTML, so it’s not as confusing as involving routes and controllers.

I also have a basic comprehension of Omeka-S PHP functions and variables, at least in terms of the theme.

So in my layout.phtml, which is really the template (head, header, and footer, with a “fill in” spot for the main content area [div id=“content”])…

I use this code to determine what page is currently open on the site:

//echo $_SERVER['REQUEST_URI'];  
$whatPage = $_SERVER['REQUEST_URI'];

The echo is for debugging, if you need to see what value is inside that variable. With CleanURL, I was getting values like “/home” and “/codex”.

So then, being familiar with themes and HTML, I placed a bit of logic within the

if($whatPage=="/codex") {
echo '<div class="blocks">';
echo $this->partial('common/advanced-search/properties-codex');
echo '&nbsp;<br><p><br></div>';

Now I can have a blank page (created in Site > Pages) and then insert any “partial” I want to create - including custom HTML forms and modified search forms (hard to do inside Omeka-S, even with Blocks Plus, Collecting, etc.).

It’s important to note WHERE exactly you place that code. In my case, it goes directly under here. It’s unlikely if you try to follow these instructions that you will have the exact same theme structure - so you’ll need to experiment a little bit, and perhaps get someone who can work with HTML/CSS to give you a better idea of exactly where to insert your block.

        <div id="content" role="main">
            <?php if($_SERVER['REQUEST_URI'] != $site->url()){ ?>
                    <?php echo $this->content; ?>
            <?php } else { ?>         
                <?php echo $this->content; ?>
            <?php } ?>

1 Like