vendredi 7 septembre 2018

Is it possible in Laravel to store a property in one route and use in another? (same class - different methods for different routes)

class MyController extends Controller {
    private $data;

    index() {

        $data = (object)[];

        $this->data = $data;

        return compact();
    }

    otherRouteMethod() {
        dd($this->data);
    }
}

ROUTES

Route::get('/my', MyController::class . '@index')->name('my.overview');
Route::get('/my/sub_route', MyController::class . '@otherRouteMethod');

Why is the following happening?

  • Both routes work
  • dd($this->data) prints "null" as it was not set in index()
  • using public modifier for $data leads to the same issue

Is it because Laravel makes a new instance of the class on every route change?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2NUTLv3
via IFTTT

Aucun commentaire:

Enregistrer un commentaire