/home/mip/mip/app/Http/Controllers/Website/PostController.php
<?php

namespace QxCMS\Http\Controllers\Website;

use QxCMS\Http\Controllers\Controller;
use QxCMS\Modules\Client\Repositories\Posts\PostsRepositoryInterface as Posts;
use QxCMS\Modules\Client\Repositories\JobOpening\JobOpeningRepositoryInterface as Job;
use QxCMS\Modules\Client\Repositories\Posts\PostCategoryRepositoryInterface as Category;
use Illuminate\Http\Request;
use client;
use DB;
use App\User;

class PostController extends Controller
{
    protected $posts;
    protected $job;
    protected $category;

    public function __construct(Posts $posts, Job $job, Category $category)
    {
        $this->posts = $posts;     
        $this->job = $job;
        $this->category = $category;
    }

    public function index(){
        $data['posts'] = $this->posts->getAllPost();
        $data['latestPosts'] = $this->posts->getLatestPost();
        $data['allPages'] = $this->posts->getAllPage();
        $data['latestPosts'] = $this->posts->getLatestPost();
        $data['latestJobs'] = $this->job->getLatestJob();
        return view('website.index', $data);
    }

    public function posts($category, $slug)
    {
        $data['posts'] = $this->posts->findBySlug($slug);
        $data['allPages'] = $this->posts->getAllPage();
        $data['latestPostByCategory'] = $this->posts->getLatestByCategory($category);
        $view = $this->viewFactory($slug, 'post');
        return view($view, $data);
    }

    public function pages(Request $request, $slug, $limit = 4)
    {
        $data['allPages'] = $this->posts->getAllPage();
        $data['specificPages'] = $this->posts->findBySlug($slug);
        $data['latestPosts'] = $this->posts->getLatestPost();
        $data['latestJobs'] = $this->job->getLatestJob();
        $data['jobpages'] = $this->job->findBySlug($slug);
        $data['postpages'] = $this->posts->findBySlug($slug);
        $data['latestPostByCategory'] = $this->posts->getLatestByCategory('mip');
        if ($request->get('slug')) {
           $data['jobapply'] = $this->job->findBySlug($request->get('slug'));
        }
        $view = $this->viewFactory($slug, 'page');
        return view($view, $data);
    }

    protected function viewFactory($category, $type)
    {
        $view = "website.".$type."-{$category}";
        if (view()->exists($view))
        {
            return $view;
        }

        return 'website.includes.'.$type.'-default';
    }
}