#Helper function says "unreachable code"

1 messages · Page 1 of 1 (latest)

vapid cobalt
#

Hi, so i have made a Helper. The steps i followed are:

  1. created Helpers folder in /App
  2. Created Activity_log.php in /App/Helpers
  3. Added following code:
<?php

namespace App\Helpers;
use Request;
use App\Models\Website_activity_log as Activity_log_model;


class Activity_log
{
    public static function add_to_log($subject) 
    {

        $log = [];
        $log["subject"] = $subject;
        $log["url"] =  Request::fullUrl();
        $log["method"] = Request::method();
        $log["ip"] = Request::ip();
        $log["agent"] = Request::header("user-agent");
        $log["username"] = auth()->user()->name;
        Activity_log_model::create($log);

    }

}```

4) Added `'Activity_log' => App\Helpers\Activity_log::class` to `aliases` in `/config/app.php`
5) Runned `composer dump-autoload`
6) inside a Controller, added `use App\Helpers\Activity_log;` 
7) Added in the same controller: `Activity_log::add_to_log("Testing 1 2 3");`

But it says: "Unreachable code detectedPHP(PHP0419)". Who can help me?
sullen herald
#

You've simply made a namespaced class with static methods. There is no reason you should be adding anything to your app.php config

#

Aliases are for services bound to the container. Using a class with static methods is not using a bound service implementation

vapid cobalt
#

Ohw. Then i will remove that. But except that, why doesn't it work?

sullen herald
#

I'd imagine that's exactly why your code isn't running. You've halted the framework from booting as you've told it to use an alias for a class that isn't actually bound to anything.

vapid cobalt
#

So i've removed 'Activity_log' => App\Helpers\Activity_log::class from the app.php. I did run composer dump-autoload but still getting the error.

sullen herald
#

Is your config cached? (Which shouldn't ever be when developing locally)

#

Try clearing caches via php artisan optimize:clear

vapid cobalt
#

Nope, still the same error

sullen herald
#

Error when running artisan and or when accessing your endpoint that uses the helper?

vapid cobalt
#

I have this. It doesn't execute the helper and Artisan doesn't drop an error

#

I see online that people add somethin to the composer.json file. Do i need to do that also?

sullen herald
#

That's an IDE warning, but does the code actually work?

vapid cobalt
#

Lemme return something, brb

#

Ahhh

sullen herald
vapid cobalt
#

Ahh it works!

sullen herald
#

Might want to reopen IDE

#

Or clear caches

vapid cobalt
#

Really thankfull for youre amazing help ❤️

sullen herald
#

Invalidate caches - if using phpstorm