Laravel Route get controller's construct without method - php

Route::get('customer/{id}', 'Customer#method'); <-- want to call construct not method
class Customer extends Controller
{
public function __construct(){
echo 123456;
}
I'm new in laravel
I try to call __construct from my controller without method, but I got error, is anyone know how to do it?

Try to do like this
Define route
Route::resource('customer/{id}', 'Customer');
In your Customer Controller
use Route;
public function __construct()
{
$id = Route::current()->getParameter('id');
dd($id);
}

Related

Can't access property of extended class with new instance throught ajax Laravel

I will give below code example to better explain:
class BaseController extends Controller
{
public $globalCurrencies;
public $currentCurrency;
public $globalLanguages;
public $currentLanguage;
public function __construct()
{
$this->globalCurrencies = $this->getCurrencies();
$this->globalLanguages = $this->getLanguages();
$this->middleware(function ($request, $next) {
$this->currentCurrency = $this->getCurrentCurrency();
$this->currentLanguage = $this->getCurrentLanguage();
return $next($request);
});
}
CartController
class CartController extends BaseController
{
public function __construct()
{
parent::__construct();
}
BaseController sets up base variables for the app. The cart is using some of them like (current currency). Some of the variables are session based so in base construct there is middleware used to get session data in the constructor). For this part, everything works and cart has access to baseController properties.
Problem occurs here:
class OrderController extends BaseController
{
public function loadPaymentsAndDelivery(Request $request)
{
$cart = new Cart;
dd($cart->globalCurrencies) // WORKS
dd($cart->currentCurrency) // NULL
}
}
Basically, on a new Cart instance, I can access every property created without middleware. Without middleware, I cannot access the session to set up the cart. Method loadPaymentsAndDelivery is loaded via ajax but I tried directly call the method and the properties were still null.
Can somebody explain why this is happening?

Laravel redirect controller to controller empty $this variable

We want to pass data from controller to another controller in Laravel (framework). In our Controller.php we got a middleware code in the __construct function, which sets a environment and person.
Code in Controller.php
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->environment = session()->get('environment');
$this->person = session()->get('person');
return $next($request);
});
}
In a different controller we pass 2 parameters (Request data) and (id of data) to another controller function. We tried many ways for pass data. On this moment we lost our $this data like environment and person, the variables environment and person are exists but empty.
We tried with:
$postController = new \App\Http\Controllers\Publication\Post\IndexController();
$postController->duplicate($request, data_get($publication, 'id'));
Or
app('App\Http\Controllers\Publication\Post\IndexController')->duplicate($request, data_get($publication, 'id'))
In Post\IndexController#duplicate we lose our $this data (empty).
We tried to get data like $this->enviroment but this variables are empty.
Anyone has any idea how to pass data with the variables filled by the middleware?
You can use the power of Container
Code in Controller.php
public function __construct()
{
$this->middleware(function ($request, $next) {
app()->instance('person', session()->get('person'));
app()->instance('environment', session()->get('environment'));
return $next($request);
});
}
In another controller:
<?php
namespace App\Http\Controllers;
class DupeController extends Controller
{
public function index()
{
dd(app('person'));
}
}
Just make sure if the "another controller" has it's own constructor, call the parent constructor, you your 'person' and 'environment' instance would be available in that controller.
<?php
namespace App\Http\Controllers;
class DupeController extends Controller
{
public function __construct()
{
parent::__construct();
// DO MAGIC
}
public function index()
{
dd(app('person'));
}
}
But I gotta tell you the truth, this is a bad practice. I just want to show you that something bad like this is possible. Try another approach like service injection to the controller using dependency injection technique and mark that service as a singleton, so container will resolve the same instance for every resolution (one instance per request).

How to access a function in every controller laravel

I can access a function in every view like this.
In my AppServiceProvide Code
public function boot()
{
$post = Post::latest()->first();
View::share(compact('post'));
}
How can i access it in every controller?
What is the best way to call a function in every controller,as i need to make sure latest record from database.
**You can write in model also using model object**
public function YourFunction(){
return "data";
}
**call it in your controller like this make model object suppose your
model name is YourModel in controller you can call like this**
$model = new YourModel();
$data = $model->YourFunction(); //calling method in controller

call model function from another model in laravel

how to call model method in another model, example
I have code like this
/model/user.php
public function get_token_by_id($id){
//some code
}
i want call in my another model
/model/restaurant
App::bind('user','user');
class RestaurantController extends BaseController {
public function __construct(user $modelUser){
$this->modelUser = $modelUser;
}
public function getUser(){
$someVar = $this->modelUser->get_token_by_id($id);
}
}
But i get an error
Call to a member function get_token_by_id() on a non-object
how to fix it?
Well... that's because $this->modelUser is a non object !
To be more precise, $this->modelUser returns null or something like that (try a var_dump($this->modelUser)). It could be because your model doesn't have the attribute declaration (protected $modelUser) or because you don't pass the right variable into the constructor.

Can't access property in abstract class via sub class

I am trying to build an abstract base controller that will extend all other controllers. So far I have something like:
abstract class BaseController {
protected $view;
protected $user;
public function __construct() {
$this->view = new View; //So a view is accessible to subclasses via $this->view->set();
$this->user = new User; //So I can check $this->user->hasPermission('is_admin');
}
abstract function index();
}
class UserController extends BaseController {
public function index() {}
public function login() {
if($this->user->isLoggedin()) {
redirect to my account
}
else {
$this->view->set('page_title', "User Login");
$this->view->set('sidebar', $sidebar); //contains sidebar HTML
$this->view->set('content', $content); //build main page HTML
$this->view->render();
}
}
}
The problem i get is I get errors like this:
Call to a member function set() on a non-object in C:\xampp\htdocs\program\core\controllers\admin.controller.php on line 44
If I put the $user and $views properties in the main controller (ie UserController), everything works fine. But I only want to set up these objects once (in the base controller) and not have to add $this->view = new View; in all my controllers.
FIXED: I overrode my constructors and I thought you couldn't call parent::__construct() on abstract classes.
What you are trying to do should work. Make sure you aren't covering up your constructor in UserController. (i.e., if it has a constructor, it needs to call its parent constructor.)
Otherwise, do some debugging to see where $this->view is being reset.
Your code works for me. You are either overriding your __construct() method in UserController, or you are overridding the view field with something other than a View object.
What you have in this form would work.

Categories