How to call non-static method using laravel alias - php

I have a custom helper class and ill set the alias for that class for access to the blade file(frontend). I can access the static methods referring to the alias::method but I need to access the nonstatic methods from the view. how can I do that?
'Access' => App\Services\Access::class,
Two functions in Access class.
public function getPermissions()
public static function getUser()
I can easily access the static function using Access::getUser()
So How can access the non-static functions?

If you just want to access it in your view, you can share a new instance of Access to all your views :
In the boot() method of your AppServiceProvider :
View::share('access', (new App\Services\Access));
then in your blade #foreach($access->getPermissions() as $permission)

You can inject an instance of this class into your view:
#inject('access', Access::class)
Now you can access all those methods via $access->whatEverMethod(). Otherwise you will need a Facade or deal with the magic methods to call the non static method on the class itself.

Related

In yii why we don't use action static

When we create an action in controller then why we don't use it static
Public static function actionLogin(){
}
$this
The whole application get the $this from controller classes, so we cannot use the static keyword.
Because the controller is being instantiated and the actions might access properties of the controller.

Does Laravel Framework instantiate every model class at boot?

With, where, hasMany etc. methods of Eloquent class are not static.
However we call these functions like that:
// Post is a child of Model class.
Post::where(...); // we don't use New keyword.
So, does Laravel Framework initiate all Model instances before we call their methods?
In the Eloquent's Model class, you have the below function which handles the static method calls dynamically.
public static function __callStatic($method, $parameters)
{
return (new static)->$method(...$parameters);
}
As you can see, it creates an instance of the Model class on which a non static method is invoked statically, then invokes that method on the instance.

Laravel custom Helper class -- can't call non-static methods from other classes

I want to use the Config::get() method in my custom Helper class, but always get an error.
At the top of a Helper.php file I have the following:
use \Illuminate\Config\Repository as Config;
Then, I have a public static function in which I want to get use the Config::get() method to grab a config setting. For simplicity, let's pretend the function is:
public static function getURL() {
return Config::get('assets.url');
}
I have an assets.php file with this url variable set. The Config::get('assets.url') method works elsewhere in my site.
But when trying to use Config::get in my Helper.php file, i get this error:
Non-static method Illuminate\Config\Repository::get() should not be called statically
I obviously can't change the Config::get method to a static one. What can i do?
You can try just importing the Facade instead of trying to get the underlying class.
use Config;
Then just use Config like normal in your class.

How to load models in Laravel?

I started studying Laravel and ran into a problem using models. How to load them? For example in CodeIgniter i used it like $model = $this->load->model('some_model'). In Laravel when i call it from controller like Sites::OfUser() it work fine, but when i call Sites::getId() it says that method should be static...
Is it possible to call method without static or i need to create facades for each model?
My model looks like this:
namespace Models;
use Eloquent;
class Sites extends Eloquent {
public function scopeOfUser($query)
{}
public function getId($name)
{}
}
For static method--
$type = Sites ::scopeOfUser($query);
and if you want normal like codeingiter then use--
$model = new Sites ();
$type = $model->scopeOfUser($query);
You can of course make a static method in the model, and do some static work in it (get ID for name or whatever).
That's no problem.
However, you must declare it static if you want to use the ::, which you are doing not.
public static /* <-- this */ function getId($name)
{
// Do work
// return $result;
}
If you want to access a method with ::, you will need to make it a static method or create a Facade.
The reason why Sites::OfUser() is "working" is because you have prefixed that method with scope.
Scopes allow you to easily re-use query logic in your models. To
define a scope, simply prefix a model method with scope.
If you want to use Facades you can follow my answer here on how to create a Facade.

$this keyword in view in CodeIgniter

I'm trying to understand how $this->load->view() works inside of a view file in CodeIgniter.
The core/Controller.php is calling core/Loader.php which then calls _ci_load() which in turn does an include('/path/to/view');
Shouldn't $this refer to the Loader class at that point? How is $this referring to the controller?
By my understanding, you should have to call $this->view() inside of a view file. Not $this->load->view() because the load() function is not accessible inside of the Loader. It's a class variable of the Controller base class. i.e, $this->load =& load_class('Loader');
Please note: I'm trying to understand the CodeIgniter internals. I know perfectly well how to embed view files in other view files as a CodeIgniter user. Please do not leave answers explaining how to use $this->load().
To simplify the understanding of what $this refers to in a view, since a view is "loaded" by a controller method, the view is still run in the same scope as that method, meaning $this can have a different context depending on which class loaded it.
For example:
class Controller1 extends CI_Controller {}
In any view file loaded in this example controller, $this refers specifically to the Controller1 class, which can access CI_Controller public and protected properties/methods as well (like the Loader or Input classes, which are assigned to the load and input properties of CI_Controller) since it extends that class.
Controllers are still just plain old PHP classes. If I were to do this:
class Controller1 extends CI_Controller {
$this->foobar = 'Hello';
}
class Controller2 extends CI_Controller {
$this->foobar = 'World';
}
...if we load the same view file in any method of either of these controllers, using $this->foobar in that view file will return a different value.
Last time I checked, $this was of class CI_Loader, try var_dump($this); inside a view.
Check out:
https://github.com/EllisLab/CodeIgniter/blob/develop/system/core/Controller.php
is_loaded(); returns an array with the already loaded classnames and their aliases from the main container.
$this->load is then an instance of CI_Loader inside the controller.
Check:
https://github.com/EllisLab/CodeIgniter/blob/develop/system/core/Loader.php
Line 778

Categories