6. Routing (ルーティング)

The default routes used in the CRUD controller are accessible through the Admin class.

CRUDコントローラーで使われているデフォルトルートは Admin クラスを通してアクセスできます。

The Admin class contains two routing methods:

Admin クラスは2つのルーティングメソッドを持っています。:
  • getRoutes(): Returns the available routes (利用可能なルートを返します);
  • generateUrl($name, $options): Generates the related routes. (関連したルートを作成します)

6.1. Routing Definition (ルーティング定義)

You can set a baseRouteName property inside your Admin class, which represents the route prefix.

あなたは Admin クラス内に baseRouteName プロパティを設定することができ、 それはルートのプレフィックスを表します。

<?php
class PostAdmin extends Admin
{
    protected $baseRouteName = 'news_post_admin';
}

If no baseRouteName is defined then the Admin will pick one for you, built in the following format: ‘admin_vendor_bundlename_entityname_action’. If the Admin fails to find the best baseRouteName then a RuntimeException will be thrown.

もし baseRouteName が定義されていなければ Admin は1つを選ぶのですが、それは次の フォーマットに従います。 ‘admin_vendor_bundlename_entityname_action’ もしAdminが最適なbaseRouteNameを見つけられなければ RuntimeException がスローされます。

The same goes for the baseRoutePattern.

baseRoutePattern も同様です。

6.2. Routing usage (ルーティングの使い方)

Inside a CRUD template, a route can be generated by using the Admin class.

Admin クラスを使うことにより、CRUDテンプレート内でルートを作成することができます。
<a href="{{ admin.generateUrl('list') }}">List</a>

<a href="{{ admin.generateUrl('list', params|merge('page': 1) }}">List</a>

6.3. Create a route (ルートの作成)

You can easily register new routes by defining them in the Admin class. Only Admin routes should be registered this way. Of course this requires the related action to be defined in the controller.

Admin クラス内で定義することにより、簡単に新たなルートを登録することができます。 Adminルートだけがこの方法で登録されるべきです。もちろんこれは、関系したアクションが

コントローラー内で定義されている必要があります。

As route is always generated with the Admin context, it can only be defined by its name.

ルートはいつも Admin contextで生成されており、自身の名前でのみ定義される

<?php
use Sonata\AdminBundle\Route\RouteCollection;

class MediaAdmin extends Admin
{
    protected function configureRoutes(RouteCollection $collection)
    {
        $collection->add('duplicate');
        $collection->add('view', $this->getRouterIdParameter().'/view');
    }
}

6.4. Persistent parameters

In some cases, the interface might be required to pass the same parameters across the different Admin‘s actions. Instead of setting them in the template or doing other weird hacks, you can define a getPersistentParameters method. This method will be used when a link is being generated.

時には、異なる Admin のアクションにまたがって同じパラメータを渡すためにインターフェースが 必要になるかもしれません。それらをテンプレート側で設定したり他の変なハックをする代わりに、

getPersistentParameters メソッドを定義することができます。

このメソッドはリンクが生成されるときに使われます。

<?php
class MediaAdmin extends Admin
{
    public function getPersistentParameters()
    {
        if (!$this->getRequest()) {
            return array();
        }

        return array(
            'provider' => $this->getRequest()->get('provider'),
            'context'  => $this->getRequest()->get('context', 'default'),
        );
    }
}

// the result :
//   $admin->generateUrl('create') => /admin/module/create?context=default