ricovzelst's avatar

How to create a 'demo' website?

I've been working on a few websites using Laravel for my portfolio. Often people will have "demo"s of their websites. A user can sign in with a specific user e.g. demo:demo123 and they can do most things a real user can do, but the changes don't stick. For example, they could publish a post, they'll see it, but others won't and when they relog it's gone as well. The data never touches the database.

I have a real server and space for a database, but I want something like this to avoid abuse/spambots abusing my microblog platform "demo" by spam posting, which would slowly drain my storage & bandwidth.

Any advice?

0 likes
2 replies
LaryAI's avatar
Level 58

One way to create a "demo" website is to use Laravel's session driver to store the temporary data instead of persisting it to the database. Here's a step-by-step solution:

  1. Create a new route in your routes file (e.g., web.php) to handle the demo login and logout functionality:
Route::get('/demo/login', 'DemoController@login')->name('demo.login');
Route::post('/demo/logout', 'DemoController@logout')->name('demo.logout');
  1. Create a new controller called DemoController using the following command:
php artisan make:controller DemoController
  1. In the DemoController, define the login and logout methods:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller
{
    public function login(Request $request)
    {
        $request->session()->put('demo', true);
        // Redirect the user to the demo area
        return redirect()->route('demo.index');
    }

    public function logout(Request $request)
    {
        $request->session()->forget('demo');
        // Redirect the user back to the homepage or any other page
        return redirect()->route('home');
    }
}
  1. Create a new middleware called DemoMiddleware using the following command:
php artisan make:middleware DemoMiddleware
  1. In the DemoMiddleware, add the following code to check if the user is in the demo mode:
<?php

namespace App\Http\Middleware;

use Closure;

class DemoMiddleware
{
    public function handle($request, Closure $next)
    {
        if ($request->session()->has('demo')) {
            // User is in demo mode, allow them to proceed
            return $next($request);
        }

        // User is not in demo mode, redirect them to the login page
        return redirect()->route('demo.login');
    }
}
  1. Register the DemoMiddleware in the $routeMiddleware array in your app/Http/Kernel.php file:
protected $routeMiddleware = [
    // ...
    'demo' => \App\Http\Middleware\DemoMiddleware::class,
];
  1. Apply the demo middleware to the routes that you want to be accessible only in the demo mode. For example, if you have a PostsController with a store method that should only be accessible in the demo mode, you can do the following:
Route::post('/posts', 'PostsController@store')->middleware('demo');
  1. In your controllers or views, you can check if the user is in the demo mode using the session helper:
if (session('demo')) {
    // User is in demo mode
} else {
    // User is not in demo mode
}

This solution allows users to log in to the demo mode using the /demo/login route and log out using the /demo/logout route. The DemoMiddleware ensures that only authenticated users in the demo mode can access the routes protected by the demo middleware. The temporary data is stored in the session, so it doesn't persist in the database.

vincent15000's avatar

If it's a website only for demo purpose.

If you propose demo:demo123 to connect, you won't be able to delete all datas after the user has logged out because several visitors will use the same credentials.

What is pratice is to create a command and execute it with a cron job to refresh the entire database once a day.

Another solution is to allow users to create their own account. Then when users log out, you can fire an event to delete all datas handled by the user.

If it's a website in production with a demo functionality

You necessarily need to bind any data with the connected user so that you can delete all his datas and not all datas from the database.

1 like

Please or to participate in this conversation.