views:

381

answers:

4

Hey,

Im new to symfony and have some simple questions. I am trying to understand the module system, but I dont understand how I create the actual homepage or other pages that are not based off of a model from the db. For example, the simple about page that has static info or the homepage that is a combination of a bunch of information from different models. Can anyone help? Thanks!

+2  A: 

You can create a module, e.g. called static and create actions for every static page or only one action that delivers the page depending on a request variable. The only thing this action does is loading a template.

IMHO it would be good if symfony comes with a default module for this.

For example actions of (my custom) module static:

class staticActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    if(!$request->hasParameter('site')) {
        return sfView::ERROR;    
    }
    $this->site = $request->getParameter('site');
  }
}

With this template:

//indexSuccess.php
<?php include_partial($site) ?>

The actual statics sites are all partials.

In my routing.yml looks like this:

# static stuff
about:
  url: /about
  param: {module: static, action: index, site: about}

This way you only have to create a new partial and a new routing entry when you add a static site and you don't have to touch the PHP code.

Felix Kling
you can use $request->getParameter() directly
gpilotino
True, I edited it... Thank you.
Felix Kling
+2  A: 

First of all, modules do not have to be restricted to a model from the database. You can have a Foo module which relies on no database content, and a Bar module that is primarily based on 3 different models. The module separation is a way to logically break up your site into manageable sections. Eg an e-commerce site might have a Products module, a Categories module and a Cart module and so on.

Your last sentence can then be split into 2 parts:

1) Static information can be on any page - if it's for things like "About us" and "FAQ" etc, I personally tend to use a "default" or "home" module, and create the various actions in there vis:

./symfony generate:module appname home

and

class homeActions extends sfActions
{
  public function executeAbout(sfWebRequest $request)
  {
    // ...
  }

  public function executeFaq(sfWebRequest $request)
  {
    // ...
  }
}

with the corresponding template files (aboutSuccess.php, faqSuccess.php).

2) A page can be comprised of data from many different models - just use your preferred ORM's method of retrieving data and set it to the view ($this->data = MyModel->findByColumn(...) etc). If you mean data from different modules, then you'd probably be better off looking at partials or components for elements of a page that can be used across different modules (navigation etc). See the Symfony docs for more details on these.

richsage
Thanks! It works but how do i edit the routing.yml files so that i can access the page like host.com/about
Daniel Hertz
Great, glad it worked for you. In your routing.yml, you can add a route that just matches the 'action' part and always uses the static module. I've put an example up at http://pastebin.com/f6b55d7a7
richsage
+2  A: 

I'm used to handle static pages in this way.

First I create a new entry in apps/frontend/config/routing.yml:

page:
  url:   pages/:page
  param: { module: page, action: index }

Then I write a "page" module (apps/frontend/modules/page/actions/actions.class.php):

<?php
class pageActions extends sfActions
{
  public function executeIndex()
  {
    $this->page = $this->getRequestParameter("page");
    $this->forward404Unless($this->_partialExists($this->page));
  }

  protected function _partialExists($name)
  {
    $directory = $this->getContext()->getModuleDirectory();
    return (is_readable($directory.DIRECTORY_SEPARATOR."templates".
            DIRECTORY_SEPARATOR."_".$name.".php"));
  }
}

Last step, put in modules/page/templates/indexSuccess.php this code:

<?php include_partial($page); ?>

So all you have to do from now is to create a partial for each static page ie. apps/frontend/modules/page/templates/_home.php which you can reach at http://yousite/pages/home (without the need to add a new routing entry for every page)

gpilotino
A: 

Apart from the above, consider having a CMS for static pages, so you won't need technical savy people to mantain them or change them. This depends on the project, of course.

nacmartin