File: D:/HostingSpaces/SBogers10/slenders.komma.pro/app/Komma/Posts/PostController.php
<?php
namespace App\Komma\Posts;
use App\Komma\Base\Controller;
use App\Komma\Components\ComponentService;
use App\Komma\Posts\Models\Post;
final class PostController extends Controller
{
private $postService;
private $postPaginationKey = 'postPagination';
public function __construct(PostService $postService)
{
parent::__construct();
$this->postService = $postService;
}
/**
* @return \Illuminate\Contracts\View\View
*/
public function index()
{
// If you don't know the code_name get page through given page id through request
// $page = $this->pageService->getPage(request()->get('page_id'));
$page = $this->links->posts->node;
$componentService = app(ComponentService::class);
$components = $componentService->getViewComponents($page->translation);
$latestPost = $this->postService->getLatestPost(1)->first();
if(isset($latestPost)) {
$posts = $this->postService->getPostsPaginatedExcludeIds([$latestPost->id]);
$posts->withPath($this->links->posts->route);
}else {
$posts = collect();
}
$this->keepTrackOfPagination($this->postPaginationKey);
// Return view
return view('site.templates.posts_index',[
'page' => $page,
'components' => $components,
'links' => $this->links,
'latestPost' => $latestPost,
'posts' => $posts,
]);
}
/**
* @param Post $post
* @return \Illuminate\Contracts\View\View
*/
public function show(Post $post)
{
$post->load('translation','translations', 'sites');
// Get the page through the set links
$page = $this->links->posts->node;
$componentService = app(ComponentService::class);
$components = $componentService->getViewComponents($post->translation);
// Create previous route for better navigation UX
$previousRoute = $this->createPreviousRoute($this->postPaginationKey, $this->links->posts->route);
// Return view
return view('site.templates.posts_show',[
'page' => $page,
'post' => $post,
'components' => $components,
'links' => $this->links,
'previousRoute' => $previousRoute,
'otherPosts' => $this->postService->getNextPosts($post),
]);
}
}