Bolt\Controller\Frontend::record PHP Метод

record() публичный Метод

Controller for a single record page, like '/page/about/' or '/entry/lorum'.
public record ( Request $request, string $contenttypeslug, string $slug = '' ) : TemplateResponse
$request Symfony\Component\HttpFoundation\Request The request
$contenttypeslug string The content type slug
$slug string The content slug
Результат Bolt\Response\TemplateResponse
    public function record(Request $request, $contenttypeslug, $slug = '')
    {
        $contenttype = $this->getContentType($contenttypeslug);
        // If the contenttype is 'viewless', don't show the record page.
        if (isset($contenttype['viewless']) && $contenttype['viewless'] === true) {
            $this->abort(Response::HTTP_NOT_FOUND, "Page {$contenttypeslug}/{$slug} not found.");
            return null;
        }
        // Perhaps we don't have a slug. Let's see if we can pick up the 'id', instead.
        if (empty($slug)) {
            $slug = $request->get('id');
        }
        $slug = $this->app['slugify']->slugify($slug);
        // First, try to get it by slug.
        $content = $this->getContent($contenttype['slug'], ['slug' => $slug, 'returnsingle' => true, 'log_not_found' => !is_numeric($slug)]);
        if (!$content && is_numeric($slug)) {
            // And otherwise try getting it by ID
            $content = $this->getContent($contenttype['slug'], ['id' => $slug, 'returnsingle' => true]);
        }
        // No content, no page!
        if (!$content) {
            $this->abort(Response::HTTP_NOT_FOUND, "Page {$contenttypeslug}/{$slug} not found.");
            return null;
        }
        // Then, select which template to use, based on our 'cascading templates rules'
        $template = $this->templateChooser()->record($content);
        // Update the route attributes to change the canonical URL generated.
        if ($content->isHome() && $template === $this->getOption('general/homepage_template')) {
            $request->attributes->add(['_route' => 'homepage', '_route_params' => []]);
        } else {
            list($routeName, $routeParams) = $content->getRouteNameAndParams();
            if ($routeName) {
                $request->attributes->add(['_route' => $routeName, '_route_params' => $routeParams]);
            }
        }
        // Setting the editlink
        $this->app['editlink'] = $this->generateUrl('editcontent', ['contenttypeslug' => $contenttype['slug'], 'id' => $content->id]);
        $this->app['edittitle'] = $content->getTitle();
        // Make sure we can also access it as {{ page.title }} for pages, etc. We set these in the global scope,
        // So that they're also available in menu's and templates rendered by extensions.
        $globals = ['record' => $content, $contenttype['singular_slug'] => $content];
        return $this->render($template, [], $globals);
    }

Usage Example

 /**
  * Controller for a single record page, like '/page/about/' or '/entry/lorum'.
  *
  * @param Request $request         The request
  * @param string  $contenttypeslug The content type slug
  * @param string  $slug            The content slug
  *
  * @return Response
  */
 public function record(Request $request, $contenttypeslug, $slug = '')
 {
     $contenttype = $this->getContentType($contenttypeslug);
     $localeSlug = $this->app['translate.slug'];
     $slug = $this->app['slugify']->slugify($slug);
     if (is_numeric($slug) || !$this->app['translate.config']->isTranslateSlugs()) {
         return parent::record($request, $contenttypeslug, $slug);
     }
     $repo = $this->app['storage']->getRepository($contenttype['slug']);
     $qb = $repo->createQueryBuilder();
     $qb->select('slug')->where($localeSlug . 'slug = ?')->setParameter(0, $slug)->setMaxResults(1);
     $result = $qb->execute()->fetch();
     return parent::record($request, $contenttypeslug, $result['slug']);
 }