2

I have 3 channels and relationships between them Theme >> Goal >> Issue where multiple issues can be a part of a single goal and multiple goals make up an issue.

I am flexible with which direction the relationships go (IE can have a single relationship from Goal to Theme or a multiple relationship to select multiple goals from the theme.

What I am looking for is a URL structure like example.com/red-theme/pretty-goal/balloons where red-theme is an entry in the Themes channel, pretty-goal is an entry in the Goals channel, and balloons is an entry in the issues channel.

I have a template group for issue with a template route: /{theme:alpha_dash}/{goal:alpha_dash}/{issue:alpha_dash}/

If I go to http://example.com/red-theme/pretty-goal/balloons I can successfully use channel:entries to get the balloons entry with {segment:issue}

{exp:channel:entries channel="issues" url_title="{segment:issue}" dynamic="no" limit="1" require_entry="yes"}

However - the problem I face - is that http://example.com/foo/bar/balloons also takes me to that page.

My Questions: 1) Am I going about this the right way or is there a better way to dynamically create urls? 2) If so, then how can I do a check for the proper relationship url_title from the url segments to either redirect or throw a 404?

PS brand new to EE and appreciate any help/guidance.

1 Answer 1

3

To answer this you need to think about what you want to have happen when EE sees a URL - only with this clear can you work out what the right approach would be.

So with your example, it sounds like foo/bar/balloons and red/pretty/balloons should be resulting in different pages - but to know how we need to know how they differ: are they different pages, or similar pages with some content different? Likewise, how would the pages created in response to red/squeaky/balloons be different from /red/pretty/balloons?

EE template language lets you respond to a URL in a very flexible way. You don't have to generate the content of a template based on the URL at all if you don't want to - you can put in conditionals that look at the URL segments and make choices about what content to include within a template.

So if you stick with the route that sends all your entries to the same template - just put in some logic at the start of the template that will parse the URL and include content as required. For example:

{if segment_2 == "pretty"} ... do something ... {if:elseif segment_2 == "squeaky"} ... do something else ... {if:else} ... a default response{/if}

You can put the actions arising from the logic in the template, or if you prefer build the output from several templates and use logic to decide which components you pull in where / when.

If you can give more info about what you are actually trying to do within the template / with the output, maybe it will be easier to make suggestions of a good approach.

HTH

+++ Modified to add information about Parents of Parents +++

You can work out segment_1 and segment_2 in your example using the parents feature of the relationships field. Something like:

{exp:channel:entries channel="issues"  url_title="{segment_3}"}
  {parents field="relationship_to_issues"} {!-- this should be the name of the relationship field in the parent channel entry --}
   <p>Segment 2 name is {parents:channel_short_name}</p>
      {parents:parents channel="channel_with_themes_in" field="relationship_to_goals"}
   <p>Segment 1 name is {parents:parents:channel_short_name}</p>
      {/parents:parents}
  {/parents}
{/exp:channel:entries}

What you do with this information when you get it is up to you - but for example you could use it to redirect the page to the 'correct' URL - put this in the middle of the parents:parents tag:

{redirect='{parents:parents:channel_short_name}/{parents:channel_short_name}/{segment_3}' status_code="301"}
4
  • Gavin- thanks for the reply. I kept it fairly vague because I'm a bit unsure about my approach. My thought process was to have the template load content based on the URL segments as you describe. However, I'm having trouble with accessing grandparents/grandchildren via relationships to do that logic. I can get the url_title from the 3rd segment, but don't know how to validate that it has a grandrelationship with the 1st segment. My goal was that if foo/bar/balloons is entered, I could redirect the user to the proper URL red/pretty/balloons.
    – danbrellis
    Feb 24, 2017 at 12:50
  • Hmm. Where will the foo/bar/balloons segments come from, and do the segments in it 'mean' anything? e.g. will foo in URL mean anything that affects what you do? Sounds like you only interested in segment_3 and want to deduce segment_2 and segment_1 from relationships...? Feb 24, 2017 at 14:43
  • yeah, basically. segment_1 and segment_2 are just for show since I can get a channel entry from just the url_title in segment_3. But if someone hijacks the url I'd like to correct it for them. So yeah, if I go this direction, my ultimate question is how to get what should be in segment_1 from the relationships with segment_3. Only thing is, segment_1 and segment_3 are in different channels and are 'related' through a middleman as it were.
    – danbrellis
    Feb 24, 2017 at 17:07
  • Have updated answer with information on how to do that. Feb 25, 2017 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.