Hot answers tagged

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

Does Craft CMS have an API?

If you only need to create a JSON API for fetching existing entries, etc., check out the Element API plugin.
Brandon Kelly'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

How do I link an image to its own page?

In Craft assets do not get "their own page" by default, but you can easily create them with a Dynamic Route. So go to Settings → Routes and add a new route with a number token, e.g. assets/<number&...
carlcs's user avatar
  • 36.1k
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

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.1k
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

Looping through returns nothing after using dynamic routes to get the entry type and category

{% if not entries %} never executes, even if i enter complete nonsense in the url. You’re not ensuring that segmentCategory is a valid category slug anywhere. If it’s not, then category will get set ...
Brandon Kelly's user avatar
4 votes
Accepted

Route token that includes forward slashes

The CP route settings don’t have a token for matching multiple URI segments. However you can pull it off from craft/config/routes.php instead: return array( 'blog/(.+)' => 'path/to/template', )...
Brandon Kelly's user avatar
4 votes
Accepted

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

Make sure to check out the documentation on routing. Pay special attention to the part about action request(s). In this example, I want to call a controller method via ajax on page load. ...
Damon's user avatar
  • 4,706
4 votes
Accepted

Controller action redirection when called via route

You're mistaken about how redirectToPostedUrl works... That's designed to redirect from a form submission, when the redirect field was supplied: <input type="hidden" name="redirect" value="page/to/...
Lindsey D's user avatar
  • 23.9k
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.2k
4 votes
Accepted

Can I open templates/routes other than 503 when Craft is offline?

I don't think it's possible using the System Status switch in Settings → General. I had a similar requirement and solved it with a custom variable "maintenanceMode" in my config.php and added some ...
carlcs's user avatar
  • 36.1k
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
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.4k
3 votes

Can I open one template/route when Craft is offline?

You can add a template called 503.html to your /templates folder. Craft will then use that template whenever the system is turned off, due to the HTTP status being set to 503 (service unavailable). ...
Mats Mikkel Rummelhoff's user avatar
3 votes
Accepted

Override a wildcard dynamic route template

Sort order matters, move the more specific route up and you should be good. Alternativly set it up in routes.php using some regular expressions.
carlcs's user avatar
  • 36.1k
3 votes
Accepted

Routes to fake a Structure

The route/template for '/itinerary/name-of-itinerary' will be defined in the section's settings, so it's the others you need to deal with. I'd add this route to your routes.php file: 'itinerary/(?P&...
Jamie Pittock's user avatar
3 votes

How do I link an image to its own page?

It's not clear what exactly you mean by 'its own page' Do you want to have an entry for each image? or a twig template that displays that image? When you access a page by default {{ entry.url }} ...
j00lz's user avatar
  • 769
3 votes
Accepted

check if a route segment matches a section entry type

Take a look at the {% exit %} tag. You should be able to do something very similar, since your search will return no results if the entry type does not exist. {% set entries = craft.entries.type(...
Aaron Berkowitz's user avatar
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.3k
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
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

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

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,011
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

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