Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
APP_NAME=Laravel
APP_NAME="Laravel & Twill Template"
APP_ENV=local
APP_KEY=
APP_DEBUG=true
Expand Down
15 changes: 15 additions & 0 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Twill\Capsules\Base\ModuleRepository;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
Expand All @@ -10,4 +11,18 @@
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

protected string|null $repositoryClass = null;

/**
* @throws \Exception
*/
public function repository(): ModuleRepository
{
if (!isset($this->repositoryClass)) {
throw new \Exception('Repository class not defined');
}

return app($this->repositoryClass);
}
}
32 changes: 32 additions & 0 deletions app/Http/Controllers/Front/Homepage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use A17\TwillTransformers\ControllerTrait;
use Illuminate\View\View as IlluminateView;
use App\Transformers\HomePage as HomepageTransformer;
use App\Twill\Capsules\Pages\Repositories\PageRepository;
use Illuminate\Contracts\View\Factory as IlluminateViewFactory;

class Homepage extends Controller
{
use ControllerTrait;

protected string $transformerClass = HomepageTransformer::class;

protected string|null $repositoryClass = PageRepository::class;

public function index(): IlluminateViewFactory|IlluminateView|string|array|null
{
$this->setTemplate('front.homepage.index');

return $this->view(['pages' => $this->repository()->get()]);
}

public function redirectToLocalizedHomepage(): RedirectResponse
{
return redirect()->to('/en');
}
}
26 changes: 26 additions & 0 deletions app/Http/Controllers/Front/Pages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use A17\TwillTransformers\ControllerTrait;
use Illuminate\View\View as IlluminateView;
use App\Twill\Capsules\Pages\Repositories\PageRepository;
use Illuminate\Contracts\View\Factory as IlluminateViewFactory;

class Pages extends Controller
{
use ControllerTrait;

protected string|null $repositoryClass = PageRepository::class;

public function index(): IlluminateViewFactory|IlluminateView|string|array|null
{
return $this->view(['pages' => $this->repository()->get()]);
}

public function show(mixed $_, string $slug): IlluminateViewFactory|IlluminateView|string|array|null
{
return $this->view($this->repository()->forSlug($slug));
}
}
3 changes: 3 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ class Kernel extends HttpKernel
* @var array<int, class-string|string>
*/
protected $middleware = [
\A17\HttpBasicAuth\Middleware::class,
// \App\Http\Middleware\TrustHosts::class,
\App\Http\Middleware\TrustProxies::class,
\Fruitcake\Cors\HandleCors::class,
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\A17\Localization\Middleware::class,
];

/**
Expand Down Expand Up @@ -63,5 +65,6 @@ class Kernel extends HttpKernel
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'url.slash' => \App\Http\Middleware\RemoveTrailingSlashMiddleware::class,
];
}
26 changes: 26 additions & 0 deletions app/Http/Middleware/RemoveTrailingSlashMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;

class RemoveTrailingSlashMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if (preg_match('/.+\/$/', $request->getRequestUri())) {
return Redirect::to(rtrim($request->getRequestUri(), '/'), 301);
}

return $next($request);
}
}
8 changes: 7 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use A17\Localization\Support\Helpers;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -13,7 +14,7 @@ class AppServiceProvider extends ServiceProvider
*/
public function register()
{
//
$this->loadHelpers();
}

/**
Expand All @@ -25,4 +26,9 @@ public function boot()
{
//
}

private function loadHelpers()
{
Helpers::loadGlobalHelpers();
}
}
5 changes: 2 additions & 3 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class RouteServiceProvider extends ServiceProvider
*
* @var string
*/
public const HOME = '/home';
public const HOME = '/';

/**
* Define your route model bindings, pattern filters, and other route configuration.
Expand All @@ -33,8 +33,7 @@ public function boot()
->prefix('api')
->group(base_path('routes/api.php'));

Route::middleware('web')
->group(base_path('routes/web.php'));
Route::middleware('web')->group(base_path('routes/front.php'));
});
}

Expand Down
10 changes: 10 additions & 0 deletions app/Support/Constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Support;

class Constants
{
const WYSIWYG_ALLOWED_TAGS = '<p><a><br><strong><em><ul><li>';

const TRANSLATABLE_URL_PARAMETERS = '';
}
25 changes: 25 additions & 0 deletions app/Transformers/A17.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Transformers;

/**
* @property array $translations
* @property array $page
*/
class A17 extends Transformer
{
public function transform(): array
{
$a17 = $this->page['a17'] ?? [];

return to_array($a17) + [
'l11n' => $this->translations,

'page' => $this->page,

'auth' => [],

'config' => [],
];
}
}
99 changes: 99 additions & 0 deletions app/Transformers/Head.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace App\Transformers;

use Illuminate\Support\Collection;

class Head extends Transformer
{
private array|Collection|null $transformedData = null;

private Transformer $__this;

public function transform(): array
{
$this->transformedData = $this->getData()['transformedData'];

$this->__this = $this->getData()['__this'];

$image = $this->transformSeoImage();

return [
'seo' => [
'title' => $this->__this->seo_title,

'description' => $this->__this->seo_description,
],

'urls' => $this->transformSeoUrls(),

'image' => ['url' => $image['src'] ?? null],

'twitter' => $this->transformTwitter(),

'og' => $this->transformOpenGraph($image),
] + ($this->__this->seo_noindex ?? false ? ['robots' => 'noindex'] : []);
}

public function transformSeoUrls(): array
{
return [
'canonical' => $this->__this->link,

'hreflang' => $this->__this->translatedLinks,
];
}

private function transformSeoImage(): array|null
{
$image = $this->transformMedia();

if (blank($image)) {
$image =
$this->transformedData['image'] ??
($this->transformedData['cover_image'] ??
($this->transformedData['page']['image'] ??
($this->transformedData['page']['cover_image'] ?? null)));
}

return $image;
}

private function transformTwitter(): array
{
return [
'handle' => '@area_17_',
];
}

public function transformOpenGraph(array|null $image): array
{
return [
'title' => $this->seo_title ?? ($this->title ?? null),

'site-name' => config('app.site_name'),

'locale' => ['current' => 'fr'],

'type' => null,

'description' => $this->seo_description,

'url' => $this->link,

'image' => [
'url' => $image['src'] ?? null,

'secure-url' => $image['src'] ?? null,

'alt' => $image['caption'] ?? ($image['caption'] ?? null),

'type' => '', //image_mime_type($image['src'] ?? null),

'width' => $image['width'] ?? null,

'height' => $image['height'] ?? null,
],
];
}
}
18 changes: 18 additions & 0 deletions app/Transformers/HomePage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Transformers;

use Illuminate\Support\Collection;

/**
* @property array|Collection $pages
* @method array|Collection transformPage($page)
* @method array transformMoleculePage($page)
*/
class HomePage extends Transformer
{
public function transformData(array|null $data = null): array
{
return ['pages' => $this->pages?->map(fn($page) => $this->transformMoleculePage($page))];
}
}
50 changes: 50 additions & 0 deletions app/Transformers/Languages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Transformers;

class Languages extends Transformer
{
public function transform(): array
{
return locales()
->map(fn($locale) => $this->transformLocale($locale))
->filter()
->toArray();
}

private function transformLocale(string $locale): array
{
return !empty(($url = $this->getUrl($this->getTransformerData(), $locale)))
? [
'label' => $locale,
'locale' => $locale,
'url' => $url,
'is_active' => locale() === $locale,
]
: [];
}

private function getTransformerData(): mixed
{
$data = $this->getData() ?? ($this->__data ?? null);

return filled($data['data'] ?? null) ? $data['data'] : $data;
}

private function getUrl(mixed $object, string $locale): ?string
{
$url = null;

if ($object && isset($object->translatedLinks)) {
foreach ($object->translatedLinks as $language => $link) {
if (substr($language, 0, 2) == $locale) {
$url = $link;

break;
}
}
}

return !empty($url) ? $url : null;
}
}
Loading