I have a helper file located at
app/Helpers/Navigation.php
Helper file with namespace:
<?php
namespace App\Helpers;
class Navigation
{
public static function isActiveRoute($route, $output = 'active')
{
if (Route::currentRouteName() == $route) {
return $output;
}
}
}
i wanted to autoload this file . So in my composer.json i have this:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/Helpers/Navigation.php"
]
},
In my view i want to do this:
<li class="{{ isActiveRoute('main') }}">
But i get the error:
Call to undefined function isActiveRoute()
Not sure what i'm doing wrong. I did composer dumpautoload when i changed the composer file. I tried installing composer again, that also didn't change anything.
i had the same issue, i'm assuming that you are using inspinia laravel version, so the problem is that they forgot to remove the file app/Helpers/Navigation.php
if you look the AppServiceProvider they are using the one in '/../Http/Helpers/Helpers.php'
if you want to do Navigation::isActiveRoute then use the class file
but if you want to use {{ isActiveRoute('youRouteName') }}
then you need to use the functions in '/../Http/Helpers/Helpers.php' and there is no need to use de composer.json (that was propossed in another solution for another problem)
i know i have the same feeling...
ps: Please sorry about my english
For a helpers
file you don't want to be using a class. You would jut define the functions you want to use.
Also, it is good practice to wrap your function in a check to make sure that function doesn't already exist.
Replace the content of you Naviation.php
with:
<?php
if (! function_exists('isActiveRoute')) {
/**
* [Description of this function]
*
* @param $route
* @param string $output
* @return string
*/
function isActiveRoute($route, $output = 'active')
{
if (Route::currentRouteName() == $route) {
return $output;
}
}
}
Hope this helps!
When your helper file is a class then there is no need to autoload it.
Just create an alias in config/app.php
as:
'aliases' => [
...
'NavigationHelper' => App\Helpers\Navigation::class,
...
Use it in your Blade template as:
<li class="{{ NavigationHelper::isActiveRoute('main') }}">
At last, you can remove the following code from composer.json
file and the run composer dumpautoload
"files": [
"app/Helpers/Navigation.php"
]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.