VGD
VGD

Reputation: 466

laravel custom classes add alias

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?

Upvotes: 4

Views: 17257

Answers (6)

Douglas.Sesar
Douglas.Sesar

Reputation: 4420

Since you mentioned that you needed a class in Blade:

In stead of setting up an alias, you could use the blade @inject function at the top of your blade file.

@inject('your_class','App\Helpers\SomeNamespace\Yourclass')

Then in the blade template:

{{$your_class->doSomething()}}

Upvotes: 1

VGD
VGD

Reputation: 466

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.

Upvotes: 8

Martin Bean
Martin Bean

Reputation: 39389

What you’re looking for is Façades.

You create a Façade class that contains a reference to your class’s binding in the service container, and that’s what you use in the aliases array in your config/app.php file.

Documentation: http://laravel.com/docs/5.1/facades

Upvotes: 0

Iamzozo
Iamzozo

Reputation: 2358

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() }}

Upvotes: 1

Borja Marco
Borja Marco

Reputation: 54

Did you tried with a slash?

{{ \Requirement::Test() }}

EDIT: grammar

Upvotes: 0

aldrin27
aldrin27

Reputation: 3407

Try this if this works:

In your controller:

 $whateveryour_variable = Requirement::Test();
 return view('yourview',compact('whateveryour_variable'));

In your View:

   {{$whateveryour_variable}}

You could fetch the data in your db then store it in a variable then pass that in your view.

Upvotes: 0

Related Questions