Custom Home page sonata page

隐身守侯 提交于 2019-12-08 09:50:16

问题


I create my custom controller for the page home.

controller:

class FrontPageController extends Controller
{
    public function homeAction()
    {
         return $this->render('FrontPageBundle:Page:home.html.twig');
    }
}

routing.yml

front_page_home:
   path: /
   defaults: { _controller: FrontPageBundle:FrontPage:home }

but the url to my controller redirects to the controller sonata.page.page_service_manager:execute

route: "page_slug"


回答1:


I found a solution but I don't know if it is good practice or not.

I create my customer RoutePageGenerator class and edit the class which will change the route to the homepage. Like this:

 // Iterate over declared routes from the routing mechanism
    foreach ($this->router->getRouteCollection()->all() as $name => $route) {

        if($route->getPath() === "/")
        {
            $name = trim($name);
            $root = $this->pageManager->create(array(
                'routeName'     => $name,
                'name'          => $name,
                'url'           => $route->getPath(),
                'site'          => $site,
                'requestMethod' => isset($requirements['_method']) ? $requirements['_method'] : 'GET|POST|HEAD|DELETE|PUT',
                'slug'          => '/',
            ));
        }

I delete this part of code:

        $root = $this->pageManager->getPageByUrl($site, '/');

    // no root url for the given website, create one
    if (!$root) {
        $root = $this->pageManager->create(array(
            'routeName'     => PageInterface::PAGE_ROUTE_CMS_NAME,
            'name'          => 'Homepage',
            'url'           => '/',
            'site'          => $site,
            'requestMethod' => isset($requirements['_method']) ? $requirements['_method'] : 'GET|POST|HEAD|DELETE|PUT',
            'slug'          => '/',
        ));

        $this->pageManager->save($root);
    }

I create my customer UpdateCoreRoutesCommand and I call my RoutePageGenerator:

just execute this command php app/console sonata:page:create-snapshots --site=1 and it works.



来源:https://stackoverflow.com/questions/32046565/custom-home-page-sonata-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!