Skip to content

Commit

Permalink
Proofreading chapter 2 of the docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverryan authored and ornicar committed Oct 25, 2010
1 parent 850e6c0 commit 857c5f0
Showing 1 changed file with 48 additions and 33 deletions.
81 changes: 48 additions & 33 deletions Resources/doc/02-Integrate-With-Symfony.markdown
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
Integrate menus with Symfony2
==========================
Using menus with Symfony2
=========================

Bundle\MenuBundle\Menu and Bundle\MenuBundle\MenuItem are perfectly decoupled from Symfony2.
They can be used in any PHP 5.3 project.
The core menu classes of the MenuBundle, `Bundle\MenuBundle\Menu` and
`Bundle\MenuBundle\MenuItem`, are perfectly decoupled from Symfony2 and
can be used in any PHP 5.3 project.

This bundle provides other classes that ease the integration of menus with a Symfony2 project.
This bundle also provides several classes that ease the integration of
menus within a Symfony2 project.

## Make your menu a service

There a lot of benefits making a menu a service. Its logic is then self-contained, and it can be accessed from any place of the project.
There a lot of benefits to making a menu a service. Its logic is then
self-contained,and it can be accessed from anywhere in the project.

### Create your menu class

// src/Bundle/MyBundle/Menu/MainMenu.php
// src/Application/MyBundle/Menu/MainMenu.php
<?php
namespace Bundle\MyBundle\Menu;
namespace Application\MyBundle\Menu;
use Bundle\MenuBundle\Menu;
use Symfony\Component\Routing\Router;

Expand All @@ -34,11 +37,14 @@ It requires a Symfony Router in order to generate uris.

### Declare and configure your menu service

# src/Bundle/MyBundle/Resources/config/menu.xml
Next, declare you menu service class via configuration. An example in XML
is shown below:

# src/Application/MyBundle/Resources/config/menu.xml
...

<parameters>
<parameter key="menu.main.class">Bundle\MyBundle\Menu\MainMenu</parameter>
<parameter key="menu.main.class">Application\MyBundle\Menu\MainMenu</parameter>
</parameters>

<services>
Expand All @@ -49,30 +55,39 @@ It requires a Symfony Router in order to generate uris.

...

If you include the menu configuration in your bundle (as shown above), you'll
need to include it as a resource in your base configuration:

# app/config/config.xml
...

<import resource="MyBundle/Resources/config/menu.xml" />

### Access the menu service

You can now access your menu like any Symfony service:
You can now access your menu like any Symfony service:

$menu = $container->get('menu.main');
$menu = $container->get('menu.main');

From a controller it's even easier:
From a controller, it's even easier:

$menu = $this['menu.main']
$menu = $this['menu.main']

The menu is loazy loaded, and will construct its children the first time you access it.
The menu is lazy loaded, and will construct its children the first time
you access it.

## Create a template helper for your menu

You will probably need to access the menu from a template.
You may render a whole action to get the menu from a controller class,
pass it to a template and the render it.
But it would be a bit overkill. You can easily create a Symfony template helper instead.
This bundle provides a generic class for menu template helpers, all you need is
to declare the helper.
You _could_ render an entire action to get the menu from a controller class,
pass it to a template and the render it. But it would be a bit overkill.
You can easily create a Symfony template helper instead. This bundle
provides a generic class for menu template helpers, all you need to do is
declare the helper.

### Declare your menu template helper

# src/Bundle/MyBundle/Resources/config/menu.xml
# src/Application/MyBundle/Resources/config/menu.xml
<service id="templating.helper.main_menu" class="%templating.helper.menu.class%">
<tag name="templating.helper" alias="main_menu" />
<argument type="service" id="menu.main" />
Expand All @@ -93,12 +108,12 @@ Or manipulate it:
$view['main_menu']['Home']->setLabel('<span>Home</span>');
$view['main_menu']['Home']->setIsCurrent(true);


## Customize your Menu

If you want to customize the way your menu are rendered, just create a custom MenuItem class
If you want to customize the way your menu are rendered, just create a
custom `MenuItem` class

# src/Bundle/MyBundle/Menu/MyCustomMenuItem.php
# src/Application/MyBundle/Menu/MyCustomMenuItem.php
<?php
namespace Application\MyBundle\Menu;
use Bundle\MenuBundle\MenuItem;
Expand All @@ -125,32 +140,32 @@ If you want to customize the way your menu are rendered, just create a custom Me
}
}

This example override the renderLink method.
Then use your new CustomMenuItem as the default item class in your MainMenu
This example overrides the `renderLink()` method. You can then use the new
`CustomMenuItem` class as the default item class in your `MainMenu`:

// src/Bundle/MyBundle/Menu/MainMenu.php
// src/Application/MyBundle/Menu/MainMenu.php
<?php
namespace Bundle\MyBundle\Menu;
namespace Application\MyBundle\Menu;
use Bundle\MenuBundle\Menu;
use Symfony\Component\Routing\Router;


class MainMenu extends Menu
{
public function __construct(Router $router)
{
parent::__construct(array(), 'Bundle\MyBundle\Menu\MyCustomMenuItem');
parent::__construct(array(), 'Application\MyBundle\Menu\MyCustomMenuItem');

$this->addChild('Home', $router->generate('homepage'));
$this->addChild('Comments', $router->generate('comments'));
}
}

Or, if you want to customize each child item, pass them as an argument of the addChild method
Or, if you want to customize each child item, pass them as an argument of
the `addChild()` method:

// src/Bundle/MyBundle/Menu/MainMenu.php
// src/Application/MyBundle/Menu/MainMenu.php
<?php
namespace Bundle\MyBundle\Menu;
namespace Application\MyBundle\Menu;
use Bundle\MenuBundle\Menu;
use Symfony\Component\Routing\Router;

Expand Down

0 comments on commit 857c5f0

Please sign in to comment.