php - Laravel add parameters to the URL

808

In my Laravel app, I have a query string after each URL to let the user select multiple clients over multiple tabs in his/her browser I read that out in my controller to select the right client. So it could look something like this: https://url-to-my-website.com/module/page?client_id=1

Now you can have multiple languages per client so I want to make it so that the user has that in the URL as well. So the end result would look something like this: https://url-to-my-website.com/module/page?client_id=1&lang=en

I am using the same code as I do for the client but for some reason, it's not working. Both functions are put in separate middlewares which I can call on the routes that need them. This is the one for Client which works perfectly:

public function handle($request, Closure $next)
{
    $url    = url()->current();
    $needle = '?client_id=';

    if ($request->get('client_id', null) === null) {
        if (mb_stripos($url, $needle) === false) {
            $lastUrl = url()->previous();

            if (mb_stripos($lastUrl, $needle) !== false) {
                $parameter = mb_substr($lastUrl, mb_stripos($lastUrl, $needle));

                $parameter = explode('&', $parameter)[0];

                $request->merge(['client_id' => mb_substr($parameter, mb_strlen($needle))]);
            } elseif (access()->getCurrentClient()) {
                $request->merge(['client_id' => access()->getCurrentClient()->client_id]);
            }
        }
    }

    return $next($request);
}

This is the one for Language which doesn't work:

public function handle($request, Closure $next)
{
    $url    = url()->current();
    $needle = '&lang=';

    if ($request->get('lang', null) === null) {
        if (mb_stripos($url, $needle) === false) {
            $lastUrl = url()->previous();

            if (mb_stripos($lastUrl, $needle) !== false) {
                $parameter = mb_substr($lastUrl, mb_stripos($lastUrl, $needle));

                $parameter = explode('&', $parameter)[0];

                $request->merge(['lang' => mb_substr($parameter, mb_strlen($needle))]);
            } elseif (access()->getCurrentStoreView()) {
                $request->merge(['lang' => access()->getCurrentLanguage()->code]);
            }
        }
    }

    return $next($request);
}

Any pointers will be appreciated, thanks in advance.

People are also looking for solutions to the problem: php - Unable to Receive PayPal IPN Response in Code Igniter

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.