Skip to main content
OverflowAI is here! AI power for your Stack Overflow for Teams knowledge community. Learn more
7 votes
Accepted

Routing within a single page app integration

To pull this off, first you’ll have to make sure all of your Element API endpoints begin with something consistent, like api/ or data/, etc. Then create a route in craft/config/routes.php that will ...
Brandon Kelly's user avatar
7 votes
Accepted

Get entry associated with current uri

Surprisingly, the Craft docs aren't super clear on this so I'll post a little info. When you set up a Section in the Control Panel, you set the URI Format and Template fields to map to an entry ...
Sam Hernandez's user avatar
6 votes
Accepted

Is environmental routing possible?

The routes.php config file doesn't support multi-environment setups in the manner that general.php or db.php does – in other words, your example code using a multidimensional array would throw an ...
Mats Mikkel Rummelhoff's user avatar
6 votes

Routing within a single page app integration

In case someone uses Craft 3, this should work: <? return array( '<url:(?!api\/).*>' => ['template' => 'index'] );
David B.'s user avatar
5 votes
Accepted

Prettify search URL

You forgot to setup the routes for your search URLs. In this case I’d recommend to use an advanced route with a regular expression, as it allows to use patterns that will ignore the slashes between ...
carlcs's user avatar
  • 36.3k
5 votes
Accepted

How to setup custom site routes created by user in plugin settings

You can use the registerSiteRoutes() hook in your plugin's primary class. Assuming your custom URL formats are stored in an array in a plugin setting called customRoutes (use AttributeType::Mixed for ...
Mats Mikkel Rummelhoff's user avatar
5 votes
Accepted

Confused about tags

You told Craft to look for tags/_tag And then you created templates/tags/_tags.html Note you have no s on the end in the route definition, but you do in the template name.
Jeremy Daalder's user avatar
5 votes
Accepted

Resolving the $entry variable for custom routes somewhere but a Twig template

Alright, if you didn't already create a custom plugin I suggest you to visit Pluginfactory.io to create the boilerplate. Let's assume your plugin handle is awesome-plugin, just for testing purposes. ...
Robin Schambach's user avatar
4 votes
Accepted

How to troubleshoot routes

Remove the / at the end of databases/ and it should work. Technically, index.html is not required either, so you could just use library-database-demo-section.
Brad Bell's user avatar
  • 67.5k
4 votes
Accepted

Simple template routes with plugins

This is a tricky topic for a simple answer. Off the top of my head, there are two general approaches you may fall in for this. I'll keep my answer fairly broad: URL-Enabled Elements You can create ...
Ben Parizek's user avatar
  • 13.5k
4 votes
Accepted

Redirect entry type to another domain

The reason that you get that odd URL with the {% redirect %} tag is just because there's some whitespace at the beginning of the value returned from your helpers.msuLink() macro. Using the |trim ...
Mats Mikkel Rummelhoff's user avatar
3 votes

Prettify search URL

If you go to Settings > Routes in the CP you're able to add routes with wildcard characters like: /search/* and then point that to a template. You can also register site routes in a plugin if ...
KnutSv's user avatar
  • 268
3 votes
Accepted

URL drops :8888 from localhost

I would try and put the port in your siteUrl config param: 'siteUrl' => 'http://localhost:8888', Alternatively you might be able to use this though haven't tested it (I believe the php server ...
joep's user avatar
  • 610
3 votes
Accepted

Use of tag.slug instead of tag.title in a custom route

You do not have to change your route. Instead modify your template where you are linking the tag. Instead of using <a href="{{ siteUrl }}tags/{{ tag.title | url_encode }}">{{ tag.title }}, <...
Fyrebase's user avatar
  • 735
3 votes

Multiple URL Formats per element type (Entries, Categories)

So semantically, I don’t love the URL format you’re coming up with. I’d like to see something like /gear/{category}/{entry} This gives you a few benefits it’s semantically readable… it gives you ...
andrew.welch's user avatar
  • 11.6k
3 votes

Wildcard subdomain for posts

It’s definitely possible. Here’s the gist: Forget the whole subdomain idea, and start by getting everything working using a URI format like /client/<slug>. Make all subdomain traffic get routed ...
Brandon Kelly's user avatar
3 votes
Accepted

Variable "entrySlug" does not exist Routing

If I'm not mistaken, I don't think setting up the route via the CP does the fancy advanced rule stuff described here. You'll therefore want to do that via the config/routes.php file with something ...
Jalen Davenport's user avatar
  • 3,045
3 votes
Accepted

Good way to handle multisite fallback templates

You can create localized template files by placing them in a subfolder of templates/ named after the site handle. See https://docs.craftcms.com/v3/dev/#template-localization for a full explanation.
Brandon Kelly's user avatar
3 votes
Accepted

Routing: How can I ignore a parameter in route definition?

It sounds like you might be trying to blend Craft/Yii routing with Rewrites (if you're coming from an Apache background). I believe what Craft interprets this as is an attempt to map a request to ...
August Miller's user avatar
3 votes
Accepted

Why do i get an Invalid route error in cp when calling plugin action?

I think my other answer is also part of this one. The only specifics I'd add here is that your action is getting "double-declared," in a way. Using the action param in a request has a lot of ...
August Miller's user avatar
3 votes

How can i disable channel list url?

Craft doesn't have any default routes for the frontend. So if you're seeing anything else than a 404 page on your /services route, it's because you've put something there. The routing documentation ...
MoritzLost's user avatar
  • 11.4k
3 votes
Accepted

Syntax for URL route instead of using url parameter

Using routes You can define a route like this in your config/routes.php: return [ '<country:{slug}>/<city:{slug}>/<productType:{slug}>' => ['template' => '_category'], ]; ...
MoritzLost's user avatar
  • 11.4k
2 votes

How can I call a controller method from the front-end using ajax

For Craft 3: public function actionCheckStatus() { $this->requireAcceptsJson(); $response = [ 'status' => 'open' ]; return $this->asJson($response); }
Jeroen's user avatar
  • 308
2 votes

Controller action redirection when called via route

In case you need this for CraftCMS version 3, as I did today, the code slightly changed: $this->redirect(Craft::$app->getRequest()->referrer); This works fine with version 3.
Luke's user avatar
  • 521
2 votes

How do I redirect to a corresponding route when user switches languages?

I need something like this and found a solution: craft-languagelink UPDATE: This solution does not work for what Adam needs.
Luiz Boaretto's user avatar
2 votes

Can we define routes (non-CP) in plugins?

In Craft 3, you can accomplish this by hooking into the UrlManager::EVENT_REGISTER_SITE_URL_RULES event.
Matt Stein's user avatar
  • 4,006
2 votes

Different template for an entry in section

Zuccs, have a look at this plugin: https://straightupcraft.com/craft-plugins/template-select
Matt P's user avatar
  • 1,548
2 votes

Advanced routing descriptive URLs to certain pages/posts

So from an SEO perspective, the first URL is actually better, in that it provides even more context for the search engines to digest, index, and understand. It's also bad from an SEO perspective to ...
andrew.welch's user avatar
  • 11.6k
2 votes

URL drops :8888 from localhost

Are you required to run port 8888? If not, you can set the port to 80 in MAMP and this would resolve the issue. I know this is not really a fix for your problem but it might be a potential work-around....
Ragnarsson's user avatar
2 votes

Using the same title

You can set the URL format to be anything you want really, just setup your templates and then setup some routes; then it's all going to work automatic for your client and they never have to worry ...
Christopher Healey's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible