Jump to content

url with urlSegment

Recommended Posts


I'm quite sure to have read everything, doc and forums, about urlSegment, but I'm still not understanding how to use it. After have set my categories with this excellent post, I'm unable to fix url as I want.

Page title             |         Template             | actual url                                   |    url I want

Categ-publi         |         parent_categ      |  categ-publi/article                     | publication/article
   -- Article           |         categories           |

Publication          |        basic-template    | publication/example1               | publication/article/example1
   -- example1      |        publication          |


For which template I should activated urlSegment and in which template file I should add some code, probably something like this (which didn't work obviously)?

if($input->urlSegment1 == 'publication') {
 echo $pages->get('/publication/$categ/')->render();

} else if($input->urlSegment1) {
 // throw a 404
 throw new Wire404Exception();

Thanks so much.



Share this post

Link to post
Share on other sites

Yes, on publication. But I'm not sure if it's the good one.

Share this post

Link to post
Share on other sites

I have to leave in a minute, but I just took a look at your site (using the login details you gave me the other day). I am not sure what the contents of a valid urlsegment would look like, but can I suggest using the template editor from Tracy (I set it up for you) to make a bd($input->urlSegment1); as well as maybe a bd('test'); inside some of your conditionals to check that they are being processed as you expect. There is a lot of logic in your publications template and it's not easy to figure out what is doing what. A little debugging should help you to figure out what is what :)

  • Like 1

Share this post

Link to post
Share on other sites

Hi @adrian,

I don't know if I should post here but there something strange happening with Tracy 3.3.1 (in my local installation of PW3.0.33). Before this version, whatever to which page I try bd($input->urlSegment1), I always get " " as answer. But now I get "bookmarks" as result. But my url is now ".../en/publication/example1/bookmarks/", a really unusual and incomprehensible one. Now I have tons of pages like this ".../en/team/bookmarks/bookmarks/bookmarks/bookmarks/bookmarks/bookmarks/bookmarks/bookmarks/bookmarks/bookmarks/bookmarks/bookmarks/bookmarks/bookmarks/bookmarks/bookmarks/bookmarks/bookmarks/bookmarks/bookmarks/", with 404 error.

When I disable Tracy, url come back to normal.

But at least, now I understand a little bit the concept of urlSegment which I misunderstood... It's something we added to the regular url? In that case, it's normal I didn't get a dump answer. But I still don't understand how to change "en/categ-publi/article" to "en/publication/article" if each part of the url is not a urlsegment...

I'm lost, I think... :(


Edit: OMG, just realize I completely and totally understood urlsegment concept...


Share this post

Link to post
Share on other sites

@mel47 - I am definitely curious about the interaction you seem to be getting with Tracy and url segments, Based on your edit it sounds like you finally understand how url segments work, which is great, but we do need to figure out all those /bookmarks/ you are getting. I have seen a single /bookmark/ before but it wasn't related to Tracy. Any chance I could take a look at the server that is doing this, with instructions on what page to be on and how/where you are making that bd() call?

It's late here, so I won't be getting back to you till morning.


Share this post

Link to post
Share on other sites

@mel47 Maybe you'll want to find the requested page with this code

echo $pages->get("/publication/$categ/")->render();

Did you spot the double quote surrounding the the selector? In PHP this is called string interpolation. At runtime the $categ variable will be replace by its actual value in the selector. More about this here : PHP The Right Way. (See the Double quotes section).


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Robin S
      A new module that hasn't had a lot of testing yet. Please do your own testing before deploying on any production website.
      Custom Paths
      Allows any page to have a custom path/URL.

      The module creates a field named custom_path on install. Add the custom_path field to the template of any page you want to set a custom path for. Whatever path is entered into this field determines the path and URL of the page ($page->path and $page->url). Page numbers and URL segments are supported if these are enabled for the template, and previous custom paths are managed by PagePathHistory if that module is installed.
      The custom_path field appears on the Settings tab in Page Edit by default but there is an option in the module configuration to disable this if you want to position the field among the other template fields.
      If the custom_path field is populated for a page it should be a path that is relative to the site root and that starts with a forward slash. The module prevents the same custom path being set for more than one page.
      The custom_path value takes precedence over any ProcessWire path. You can even override the Home page by setting a custom path of "/" for a page.
      It is highly recommended to set access controls on the custom_path field so that only privileged roles can edit it: superuser-only is recommended.
      It is up to the user to set and maintain suitable custom paths for any pages where the module is in use. Make sure your custom paths are compatible with ProcessWire's $config and .htaccess settings, and if you are basing the custom path on the names of parent pages you will probably want to have a strategy for updating custom paths if parent pages are renamed or moved.
      Example hooks to Pages::saveReady
      You might want to use a Pages::saveReady hook to automatically set the custom path for some pages. Below are a couple of examples.
      1. In this example the start of the custom path is fixed but the end of the path will update dynamically according to the name of the page:
      $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template == 'my_template') { $page->custom_path = "/some-custom/path-segments/$page->name/"; } }); 2. The Custom Paths module adds a new Page::realPath method/property that can be used to get the "real" ProcessWire path to a page that might have a custom path set. In this example the custom path for news items is derived from the real ProcessWire path but a parent named "news-items" is removed:
      $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template == 'news_item') { $page->custom_path = str_replace('/news-items/', '/', $page->realPath); } }); Caveats
      The custom paths will be used automatically for links created in CKEditor fields, but if you have the "link abstraction" option enabled for CKEditor fields (Details > Markup/HTML (Content Type) > HTML Options) then you will see notices from MarkupQA warning you that it is unable to resolve the links.
      Install the Custom Paths module.
      The custom_path field is not automatically deleted when the module is uninstalled. You can delete it manually if the field is no longer needed.
    • By Aleksey Popov
      Hi! Is it possible to specify explicitly the parent section when creating a new child page with a single template?
      The idea is to save the user from the possibility of mistakenly choosing a section, but not to create unnecessary identical templates
      What I mean:
      Let's say I've got 2 parent categories with names articles & news. Each of them utilized the same admin-template (category) with the category.php file. Also, I have 2 different templates for child pages (article & news).
      Next, I need to add an article page to the articles category with the "create new" button in admin. At that moment, I must choose the category to place (because both categories use the same template). 
      To runaround this (and create a page right into the exact category without the choosing step) I should create 2 templates for each category, and setup parent-children relations for each pair of templates, right?
      But Is any way to use only the one category template and different children templates and at the same time, explicitly specify which section to use for child pages, thereby removing the process of selecting a child section?
      articles (category.php) Article page 1 (page-article.php) Article page 2 (page-article.php) Article page 3 (page-article.php) + Add a new article page here without category chooser news (category.php) News page 1 (page-news.php) News page 2 (page-news.php) News page 3 (page-news.php)  Update: found module  but I can't get it work.
    • By AndZyk
      I am currently building a intranet which will be hosted on the local network of a company. This intranet has many links to files on their fileserver with the protocol file://.
      So for example the links look like this file://domain.tld/filename.ext
      When I try to insert such a link into a URL field, I get the error, that only the protocol http:// is allowed. When I try to insert such a link into a CKEeditor link, it gets stripped out. Is it possible to insert such links into the FieldType URL and CKEditor.
      I know that I could use a FieldType Text or insert a RewriteRule in the .htaccess file, but I am looking for a more elegant solution. 😉
      Regards, Andreas
    • By stanoliver
      The following code snippet is part of my markup simple navigation and the url_redirect (url field in the backend) just works fine when I put an special custom url into the url_redirect field.
      <?php $nav = $modules->get("MarkupSimpleNavigation"); // topnav $current = $page->rootParent(); // current root page // subnav echo $nav->render(array( 'max_levels' => 2, 'item_tpl' => '<h4><a class="g8-bar-item g8-hover-black" href="{url_redirect|url}">{title}</a></h4><hr class="sidenav">', 'collapsed' => true, ), $page, $current); ?>  In my seperated breadcrumb navigation I use the following code snippet
      <?php foreach($page->parents()->append($page) as $parent) { echo "<li><a href='{$parent->url_redirect|url}'>{$parent->title}</a></li>"; } ?> Now to the problem: In my first code snippet above the
      works just fine but when I try something similiar in the second code snippet
      I produce an server error How do I have to change the second code snippet so that it works in the correct way as the first code snippet does?
    • By gregory
      Hi everyone.
      I can't see the PDF uploaded via a field called "pdf".
      I get a url like this: http://localhost:8888/mywebsite/site/assets/files/1129/test.pdf%EF%BB%BF%EF%BB%BF
      Could anyone help me? Thank you.
      <?php foreach($page->case_studies as $item) { if($item->type == 'contenuto') { echo " <a class='btn btn-primary btn-sm' target='blank' href='{$item->pdf->first()->url}' role='button'>Download</a> "; } } ?>  
  • Create New...