While working with Controllers extending SeamlessController, responses are handled both in seamless and non-seamless
mode via the respond()
function of the extended class.
Parameter name | Type | Description |
---|---|---|
$request | Illuminate\Http\Request | Request reference of the original controller |
$viewName | string | Laravel view name |
$assignedVars | array | Array of the values assigned to the view to be rendered |
$responseData | array | Array of additional values we would like to add to the response in seamless mode |
Before rendering your view based on your request type, respond()
processes passed data through $assignedVars
first.
This is handled by the generateSEOData
and generateTranslatedURLBase
functions of your SeamlessController.
SeamlessController has a function dedicated to handle SEO data, named generateSEOData()
. This function is called every
time a response is returned via the respond()
function and generates default SEO data values whenever the SEO
array
has no corresponding value in $assignedVars
.
Translation keys and values used by
generateSEOData
should be set inresources/lang/[code]/seo.php
. The sitename parameter mentioned below should also be set in .env via the SITE environment variable.
The following SEO values are checked and handled by generateSEOData()
(keys of the SEO
array in
$assignedVars
):
Parameter name | Description |
---|---|
skipTitleGeneration | If true , title values will not be changed (see title parameter for the default behaviour) |
title | If not set, the translation value of *sitename*-default-title , otherwise *sitename*-generic-title will be assigned to $SEO['title'] . The *sitename*-generic-title translation key is expected to have a :title placeholder. |
keywords | If not set, the translation value of *sitename*-default-keywords will be assigned to $SEO['keywords'] . |
description | If not set, the translation value of *sitename*-default-description will be assigned to $SEO['description'] . |
image | If not set or the assigned path is invalid, a placeholder image will be assigned to $SEO['image'] (returned by the getImage() helper function). |
GreenLight's SeamlessController - if implemented correctly - supports switching between translated languages. Since the language switcher would most likely be found outside the rendered view in seamless mode (somewhere in the masterpages probably), ajax requests have to dynamically pass translated URL's to the frontend handler, which then can update URL's on the language switcher.
Translated URL's are handled in the translatedURL
array of $assignedVars
. Controllers extending SeamlessController
have to set these values correctly, similar to this:
public function read($seo_name, Request $request) {
...
foreach(config('translatable.locales') as $language) {
$assignedVars['translatedURL'][$language]['element'] = $newsElement->translate($language)->seo_name;
}
return $this->respond($request, $viewName, $assignedVars);
}
However, this only sets the SEO alias values of the mirror URL's. Views need to know the full path, hence the need for
generateTranslatedURLBase()
, which will always be called by respond()
and generate the correct
$assignedVars['translatedURL'][$language]['base']
values, so you have no further work to do with it.
A working implementation of a Controller extending SeamlessController:
public function read($seo_name, Request $request)
{
$article = Article::where('seo_name', '=', $seo_name)->firstOrFail();
$viewName = 'pages.article'; // View name
// Setting assigned values to the pages.article view
$assignedVars = [
'article' => $article
];
// Translated URL's
foreach(config('translatable.locales') as $language) {
$assignedVars['translatedURL'][$language]['element'] = $article->translate($language)->seo_name;
}
// Meta values
if($article->meta_title) {
$assignedVars['SEO']['title'] = $article->meta_title;
} else {
$assignedVars['SEO']['title'] = $article->title;
}
$assignedVars['SEO']['description'] = $article->description;
$assignedVars['SEO']['keywords'] = $article->meta_keywords;
if($article->article_images()->first()) {
$assignedVars['SEO']['image'] = URL::to('/').$article->article_images()->first()->image_src;
}
return $this->respond($request, $viewName, $assignedVars);
}