Roots Discourse

Call function that in helpers.php from Controller/App.php

There is function that I need call in App.php (Controller) called asset_path() and its located in helpers.php

class App extends Controller
{
public function pageMeta()
{
return App\asset_path(‘path/to/assets’);
}
}

But it causes page crash. When I change return “Test Return”. The page is normal again. How to call asset_path or function that exist in helpers.php?

You probably need to namespace the function correctly.

You haven’t posted any errors, but my guess is that the error you’re seeing is about being unable to find a function. Your controller is probably in the namespace App\Controllers. If you look at helpers.php, it is in the namespace App. Since your controller is one level “deep,” you will need to put a \ at the beginning of your namespaced call to asset_path(), to return to the “root.” Otherwise, PHP will attempt to concatenate the namespaces, and will look for App\Controllers\App\asset_path(), which almost certainly does not exist. The correct call is likely \App\asset_path().