4

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?

6 Answers 6

8

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

I did as you suggested, not working - I additionally executed dump-autoload but problem remains: Class 'Requirement' not found.
Also with the alias? I tested with laravel 5.1 and it's working. Try to create a file in the app directory, name it to something, write a simple method in it: function helloWorld() {echo 'hello'} . Add to your autoload files array, then composer dump-autoload, call helloWorld() wherever - to check if it's loading files is the problem itself.
Also try with to app/Classes/Requirement.php, it could be casesensitive if you are running linux/unix
@lamzozo if that wasn't clear: calling helloWorld() worked, but not calling the class
1

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

Comments

0

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.

1 Comment

I'd prefer not to do that, since I would have to keep track about what data should be shipped with the view
0

Did you tried with a slash?

{{ \Requirement::Test() }}

EDIT: grammar

1 Comment

I did , not working - wouldn't be a solution for me anyways since I don't want any prefix, just plain <code>Requirement::Test()</code>
0

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

5 Comments

I'll try that, thank you - but shouldn't it work with a plain class as well? Just wondering...
little side note: when I put anything like exit() or echo "blabla" in register() of the Provider, nothing happens - looks like the provider is not getting loaded.
@VGD You’re still doing it wrong. You don’t add the class to the aliases array, you add the name of the façade class, that contains the reference to your class in the service container.
Either I'm dumb or I don't get what you're trying to say: My facade class which is referencing my facade is called Requirement, just like the normal class. So I am referencing the facade class in the alias.
btw when you write app/config.php you actually mean config/app.php right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.