Calling a variable or method within a static method

Asked

Viewed 34 times

0

Hi, I’m using Laravel on this app. I have a static method called get() (Which was created in a Class I did not gain access to) and inside it I need to access an external variable called $routeTo. I tried 3 things and failed. IS_MOBILE is a constant defined on another page that is stored in the Session.

Problem:

use Illuminate\Support\Facades\Route;


$routeTo = "desktop/";
if(IS_MOBILE == true){
    $routeTo = "mobile/";
}



Route::get('/', function () {
    return view( $routeTo . 'home');
});

Attempt 2: FAIL

function checkingDevice(){
    $routeTo = "desktop/";
    if(IS_MOBILE == true){
        $routeTo = "mobile/";
    }
}


Route::get('/', function () {
    checkingDevice();
    return view( $routeTo . 'home');
});

Attempt 3: FAIL

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    $routeTo = "desktop/";
    if(IS_MOBILE == true){
        $routeTo = "mobile/";
    }
    
    return view( $routeTo . 'home');
});

I believe it is a 'context/scope' problem, where the method get() cannot get the variable $routeTo or the method checkingDevice(). If anyone can help by indicating any material targeted to this is already great (the documentation of the Standard does not specifically speak of this).

  • 3

    ::get is a facade and you must do a middleware, give a search on that.

  • I could create the checkingDevice() in facade or in the .../Routing/Router.php? Or the ideal would be to create the Middleware itself?

  • it would be better and ideal to create a middleware

  • Did you ever debug IS_MOBILE with dump(IS_MOBILE) or dd(IS_MOBILE) ? Take a look here > https://stackoverflow.com/questions/34151268/how-to-show-different-view-page-for-mobile-device-laravel-5. Suggested by https://github.com/jenssegers/agent. for use on Laravel Jetstream

  • Thank you @Marcosxavier. I believe it will solve my problem. Thank you very much!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.