views:

436

answers:

1

Hey All!

So I am somewhat new to symfony but am getting better by the day thanks to the symfony websites and stackoverflow! So, I have a module called articles, where I display a list of articles the I uploaded. On the index, its just lists all of the article. BUt i was wondering how I could make it so that if I click on the author, for example, it will then take me to a page that only displays article by that author (and making the url pretty ex: articles/daniel) or by a different field such as by publication. I know how to make the queries obviously, but I don't understand if I should be making new templates for these things or just change parameters on the index page and check for them.

Thanks everyone!

+3  A: 

You can use an ORM object route to achieve this with minimal coding on your part. I'm going to assume you're on Doctrine but if you're on Propel the implementation should be very similar (probably just using sfPropelRoute as your class). In your application's config/routing.yml add your custom route:

author_articles:
  url:   /authors/:author
  param: { module: articles, action: author }
  class: sfDoctrineRoute
  options: { model: Article, type: list }

Note: I used the URL /authors/ instead of the /articles/ you requested so this route won't conflict with any of the actions in your articles module but feel free to use any URL you'd prefer in your config.

Clear your cache after saving those changes to make Symfony aware of the new route. What this has done is told your app to take all URLs matching /authors/* and pass them through the author action of your articles module, generating a list of objects that matches the :author parameter in the URL. The object route does this all automatically with just the configuration above.

Now in the author action of your articles module add:

public function executeAuthor() {
  $this->articles = $this->getRoute()->getObjects();
}

Now you have the query result in a variable for your action template. In your authorSuccess.php template loop through the $articles array as you see fit.

To link to this route from your main list using an author's name you can use the url_for helper with the route you just created to generate the full URLs dynamically:

<a href="<?php echo url_for('@author_articles?author=' . $article['author']) ?>"><?php echo $article['author'] ?></a>

That's all there is to it.

Cryo
Thanks for the great help! I am having a new problem though. Seeing as the author name or publication is often more than one word or uses unfriendly characters, i tried using slugs instead but i get new errors. I changed my routing to this:publication_articles: url: /publications/:publication_slug class: sfDoctrineRoute param: { module: article, action: publication } options: { model: Article, type: list }And made a getPublicationSlug() in the article class. But I get this error:"/publications/:publication_slug" route has some missing mandatory parameters (:publication_slug).
Daniel Hertz
I'm going to guess that you're trying to generate a URL using that route with url_for or link_to when you're getting that error. The only time I've ever seen that come up is when using the object linking: url_for('my_route', $obj). I have never successfully gotten that to work and I've tried quite a few variations on what is presented as the actual working version in the online guide (http://www.symfony-project.org/jobeet/1_2/Doctrine/en/05). Try linking by direct parameter insertion: url_for('@publication_articles?publication_slug=' . $article->getPublicationSlug()). Hope that helps.
Cryo