<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MainSiteController extends Controller
{
    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  string  $category
     * @param  string  $page
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request, $category, $page)
    {
        // Sanitize strings just in case
        $category = preg_replace('/[^a-zA-Z0-9_-]/', '', $category);
        $page = preg_replace('/[^a-zA-Z0-9_-]/', '', $page);

        // Check if the specific view exists
        $viewName = "main.{$category}.{$page}";
        if (view()->exists($viewName)) {
            // we can pass some meta data based on category and page
            $title = ucwords(str_replace('-', ' ', $page));
            return view($viewName, compact('title'));
        }

        // If not found, abort with 404
        abort(404, 'Page not found.');
    }
}
