How do different people manage urls and within their application? It's one thing that always winds up a bit messy for me. I've listed the different approaches I can think of below, but really this a question not a guide, so i'd appreciate other peoples suggestions and input.
Our Goals Should Be:
1) Each URL to be defined in one place so it's easy to change if we ever change the structure of the website.
2) We want URL generation to be simple for use in the templating engine
3) We want to be flexible to handle different types of URLs and follow SEO best practises
In Models
You could do something like this:
$article = Article::find(1);
echo $article->link();
But then what about urls that are not about a specific model, such as the domain.com/articles section? How do you manage those URLs?
Static Classes
You could use static classes like this:
echo URL::articlesSection()
echo URL::article($article)
echo URL::articleCategory($category)
But this file is going to get big very quickly. Plus it becomes inflexible, generating a URL to a category is now dependent upon having a $category object. What if you only have the id and the title of the category? That is enough to generate the URL "domain.com/category/1/foo-bar" do you really want to do another query and create another object just to create a URL? (my actual response to this would be yes, since what if you change the structure and thus need something else, it's better to be using objects).
So avoid having one massive file you could namespace like so:
echo URL\Articles::section();
echo URL\Articles::category();
echo URL\Articles::article();
Arguably this is uglier when used within a Templating engine, but i'm not so sure that's really a problem.
Actions and Routes
Laravel has a built in way using actions and routes
echo action('ArticlesController@getCategory', $params);
echo route('articleCategory', $params);
The problem with this is, you don't necessarily know what $params you want to send and you can't send an object. So you would wind up writing something like this in your views:
echo action('ArticlesController@getCategory', array('id'=>$category->id,'title'=>$category->title));
That's not very nice at all and would still need to be changed everywhere if you changed your structure too much.
A mixture of both
Looking at the source code of Laravel.io: https://github.com/LaravelIO/laravel.io/blob/master/app/views/forum/threads/index.blade.php
It uses a mixture of both. In some places it will do things like this:
href="{{ action('ForumThreadsController@getCreateThread') }}"
In other places it will do this:
href="{{ $thread->url }}""
In some places it gets really messy like so:
href="{{ action('ForumThreadsController@getIndex', '') . $queryString }}"
This may need to be changed in multiple places throughout the app if the structure ever changed too much e.g. not using query strings.
How do you do it?
This post is genuinely meant as a question. I've tried to answer it and give a bit of detail because I find that's better than posting a one line question "How do you organise your URLs".
But I don't have a conclusion to offer, simply because I do not know what the best approach is. What novel ways has everyone else found of managing their URLs and the structure of their application?