PHP code example of larasense / static-site-generation

1. Go to this page and download the library: Download larasense/static-site-generation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

larasense / static-site-generation example snippets


// config/app.php

    'providers' => ServiceProvider::defaultProviders()->merge([
        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        App\Providers\RouteServiceProvider::class,
        Larasense\StaticSiteGeneration\StaticSiteGenerationServiceProvider::class,
    ])->toArray(),


namespace App\Http\Controller;

use SSG;

class HomeController extends Controller
{

	[#SSG(url:'/')]
	public function index(Request $request)
	{
		// all processing
		// ...
		// and render the page
		return Inertia::render('product/Show', [
			'products'=> $products,
			'promos'  => $promotions,
			'Heros'   => $hero_images,
			// other info that the landing page must show
		]);
	}
}

namespace App\Http\Controller;

use Josensanchez\LaravelSSG\Attributes;

class ProductController extends Controller
{
	/**
	 *
	 * url /product/{product}
	 */
	 [#SSG(paths: 'getStaticPath')#]
	public function show(Product $product)
	{
		// all processing
		return Inertia::render('product/Show', compact('product'));
	}

	/**
	 * it generate an array like [ 'product'=>1, 'product'=>2, 'product'=>3 ]
	 * and it will use it to generate the routes
	 *  /product/1
	 *  /product/2
	 *  /product/3
	 */
	public static function getStaticPath()
	{
		return Product::where('need_to_be_pre_render', true)
			->get()
			->map(fn ($product) => ['product' => $product->id]
			->toArray();
	}
}
bash
php artisan make:controller HomeController
shell
php artisan SSG:generate