So I want to add a alias for custom classes in order to be able to use them in blade:
<?php
namespace App\Classes;
class Requirement
{
public static function Test()
{
return "hello";
}
}
In config/app.php
I added a alias like so:
...
'Requirement' => App\Classes\Requirement::class
Then, I would like to be able to call it in a blade template like
{{ Requirement::Test() }}
But the alias is not working somehow. I also tried composer dump-autoload
, but it's still not working.
BTW: Is adding custom classes like that a viable way to implement site-specific logic like retrieving and processing data from a database or is there a better approach?
Edit 1
I created Requirement.php
in app/Facades
with following content
<?php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Requirement extends Facade{
protected static function getFacadeAccessor() { return 'Requirement'; }
}
added PageContentProvider.php
in app/Providers
with the following content
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class PageContentProvider extends ServiceProvider
{
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->bind('Requirement', function($app){
return new \App\Classes\Requirement();
});
}
}
and in config/app.php
the alias
'Requirement'=>App\Facades\Requirement::class
as well as the provider
App\Providers\PageContentProvider::class
but it's still not working.
Edit 2
By adding something like
exit();
or
echo "blabla";
inside register()
, nothing changes. Does that indicate that PageContentProvider
is not even getting loaded?
Edit 3
Since the standard AppServiceProvider
gets loaded, I deleted the coresponding entry of AppServiceProvider
in config/app.php
... and it still worked! Somehow my changes don't get applied. Does anybody have solution for that?
Problem wasn't the code at all:
After realising that changes in config/app.php
didn't get applied, a simple
php artisan config:clear
fixed literally every issue I presented in my question.
Add to your composer file:
"autoload": {
//...
"files" : ["app/classes/Requirement.php"]
},
Then add to your alias as you wrote in your config/app.php
Then you will be able to use in your templates:
{{ Requirement::test() }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With