Issues (350)

app/Http/Controllers/Language.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Routing\Controller;
0 ignored issues
show
This use statement conflicts with another class in this namespace, App\Http\Controllers\Controller. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
8
class Language extends Controller
9
{
10
    /**
11
     * Set locale if it's allowed.
12
     *
13
     * @param string                   $locale
14
     * @param Request $request
15
     **/
16
    private function setLocale($locale, $request)
17
    {
18
19
        // Check if is allowed and set default locale if not
20
        if (! language()->allowed($locale)) {
21
            $locale = config('app.locale');
22
        }
23
24
        if (backpack_auth()->check()) {
25
            backpack_auth()->user()->setAttribute('locale', $locale)->save();
26
        } else {
27
            $request->session()->put('locale', $locale);
28
        }
29
    }
30
31
    /**
32
     * Set locale and return home url.
33
     *
34
     * @param string                   $locale
35
     *
36
     * @return string
37
     **/
38
    public function home($locale, Request $request)
39
    {
40
        $this->setLocale($locale, $request);
41
42
        $url = config('language.url') ? url('/'.$locale) : url('/');
43
44
        return redirect()->to($url);
45
    }
46
47
    /**
48
     * Set locale and return back.
49
     *
50
     * @param string                   $locale
51
     *
52
     * @return string
53
     **/
54
    public function back($locale, Request $request)
55
    {
56
        $this->setLocale($locale, $request);
57
58
        $session = $request->session();
59
60
        if (config('language.url')) {
61
            $previous_url = substr(str_replace(config('settings.app_url'), '', $session->previousUrl()), 7);
62
63
            if (strlen($previous_url) == 3) {
64
                $previous_url = substr($previous_url, 3);
65
            } else {
66
                $previous_url = substr($previous_url, strrpos($previous_url, '/') + 1);
67
            }
68
69
            $url = rtrim(config('settings.app_url'), '/').'/'.$locale.'/'.ltrim($previous_url, '/');
70
71
            $session->setPreviousUrl($url);
72
        }
73
74
        return redirect()->to($session->previousUrl());
75
    }
76
}
77