Autoload function with namespaces

With Sage 9, I have:

## app/Walkers/sj.php
namespace App\Walkers;
class sj_navwalker extends Walker_Nav_Menu {}



## resources/views/partials/header.blade.php


Also, tried


I get this error: Class 'App\Walkers\sj\sj_navwalker' not found

what is wrong? Thank you!

Sage9 controller follows PSR-4 rules, You have to update your class name based on PSR-4.

Check here,

Hope this helps…:+1:

1 Like

@PrecisionCoder13 is correct, you’re deviating from PSR-4 and it means that PHP can’t find your files.

The name of your class needs to match the name of your file, but here they don’t:
file: sj
class: sj_navwalker

Then when you call your class, you’re inserting an additional segment to your namespace that doesn’t exist: sj

It looks like you’ve assumed that the name of the file that contains your class adds a distinct segment to your namespace; this isn’t true. The name of the file must match the class–that’s how PHP knows what class that file contains.

So you need to fix two things:

  1. Rename your class file to sj_navwalker.php
  2. Remove sj from your call to the class: \App\Walkers\sj_navwalker()

In addition to the resources linked above, you might also check out this post I wrote a little bit ago on namespacing and autoloading:

1 Like

Thank you for the answer and references. It works and I’ve a better understanding.

Also, I had to reference Walker_Nav_Menu class in the root namespace:

class sj_navwalker extends \Walker_Nav_Menu {}

This topic was automatically closed after 42 days. New replies are no longer allowed.