Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tbergman001's avatar

Where should basic logic be placed?

Hi Everyone,

Have successfully gotten laravel up and running, published the hello world app and starting to dive deeper into a basic production prototype; however I am at a loss regarding best practice on some simple logic and where to place these items:

-IP Blocking, traditional I would have this within the html header for the page so that users not on a specific IP would be re-directed to another page (normally my "how to connect to the VPN"). Is it best practice or appropriate to place these specific small scripts in a blade file and then call them with the @yield method?

-Is there a way to define some master global variables for the entire framework?

Thanks,

0 likes
3 replies
devfrey's avatar
  1. Middleware
  2. Depends. If they're constants, a simple PHP class with public constants will work just fine. Otherwise, it depends on your use case.
martinbean's avatar
Level 80

IP Blocking

@learn-by-flying This is intercepting a HTTP request and doing something if a condition isn’t met, which makes it a perfect candidate for middleware.

You could create a class that checks the IP address of the user and redirects if it’s not in your whitelist:

class VerifyUserIsUsingVpn
{
    public function handle($request, Closure $next)
    {
        $ipAddressOfVpn = '123.123.123.123';

        if ($request->ip() === $ipAddressOfVpn) {
            return $next($request);
        }

        return redirect()->to('url-containing-connection-instructions');
    }
}

Is it best practice or appropriate to place these specific small scripts in a blade file and then call them with the @yield method?

No. Blade templates are for rendering content, not for doing complex logic like checking the user’s IP address.

Is there a way to define some master global variables for the entire framework?

I’m guessing you’re using to have a file you include with a load of variables/constants. Instead, you can organise your “global” variables into configuration files.

Without knowing what “global” variables you are wanting to set, I can’t really give an example. But the IP address of your VPN would make a good candidate for a configuration value.

1 like

Please or to participate in this conversation.