Welcome Guest, Not a member yet? Register   Sign In
Tatter\Menus basics
#1

I am trying to use Tatter\Menus and have some very basic questions. I installed Tatter\Menus via Composer at /vendor/tatter and can't figure out how to get the example menu working, MainMenu.php.

Where should I put MainMenu.php - in app/Views? Or somewhere else?

Documentation says "Since Menu is Stringable it can be used in your view or layout files as is. ".   If I create a view (eg, TatterExampleView.php), I don't understand how to access MainMenu in the view file to display the menu? I looked at the Spatie page to see if it had any additional guidance and did not see anything.

I really appreciate the work that was done to provide this library.

Code:
<? php
class MainMenu extends \Tatter\Menus\Menu
{
  public function __toString(): string
  {
    return $this->builder
    ->link(site_url('/'), 'Home')
    ->link(site_url('/about'), 'About')
    ->html('<hr>')
    ->link(site_url('/contact'), 'Contact')
    ->render();
  }
}
Reply
#2

MainMenu is a PHP class. You can put anywhere if the CI4 autoloader can autoload it.
That is you must respect PSR-4.

For example, app/Menus/MainMenu.php

And you can instantiate it and convert to string.

PHP Code:
$mainMenu = (string) new \App\Menus\MainMenu(); 
PHP Code:
echo new \App\Menus\MainMenu() 

But Tatter\Menus has a controller filter. It seems using it is easier.
https://github.com/tattersoftware/codeig...#deploying
Reply
#3

(This post was last modified: 12-27-2021, 05:47 AM by dm1. Edit Reason: Added log file contents )

(12-26-2021, 05:06 PM)kenjis Wrote: MainMenu is a PHP class. You can put anywhere if the CI4 autoloader can autoload it.
That is you must respect PSR-4.

For example, app/Menus/MainMenu.php

And you can instantiate it and convert to string.

PHP Code:
$mainMenu = (string) new \App\Menus\MainMenu(); 
PHP Code:
echo new \App\Menus\MainMenu() 

But Tatter\Menus has a controller filter. It seems using it is easier.
https://github.com/tattersoftware/codeig...#deploying
Thank you for the feedback. When I put it in app/Menus and instantiate it in a simple controller, I get an error exception 

PHP Code:
ErrorException #64

Cannot declare class Tatter\Menus\MainMenubecause the name is already in use 

Looking at the the Files tab in the debugger, I don't see multiple instances of MainMenu being loaded. Do you have any suggestions on how to troubleshoot this?

Code:
<?php
// app/Menus/MainMenu.php
namespace Tatter\Menus;

class MainMenu extends  Menu
{
  public function __toString(): string
  {
   return $this->builder
   ->link(site_url('/'), 'Home')
   ->link(site_url('/about'), 'About')
   ->html('<hr>')
   ->link(site_url('/contact'), 'Contact')
   ->render();
  }
}


Code:
<?php
// TatterMenu.php

namespace App\Controllers;

class TatterMenu extends MY_Controller
{
  public function index(){
    $mainMenu = (string) new \App\Menus\MainMenu();
  }
}

Log file contents are:
INFO - 2021-12-27 05:01:10 --> MY_Controller->MYlogger MYredirectIfNotLoggedIn App\Controllers\TatterMenu User:? IP:?
INFO - 2021-12-27 05:01:10 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2021-12-27 05:01:10 --> MY_Controller->MYlogger construct()->App\Controllers\TatterMenu User:? IP:?
CRITICAL - 2021-12-27 05:01:10 --> Cannot declare class Tatter\Menus\MainMenu, because the name is already in use
#0 [internal function]: CodeIgniter\Debug\Exceptions->shutdownHandler()
#1 {main}
INFO
#1 {main}
Reply
#4

You need learn about PHP namespaces and PSR-4.

https://www.php.net/manual/en/language.n...ionale.php
https://www.php-fig.org/psr/psr-4/
Reply
#5

Thanks for the reading recommendations which I read. I looked at all the classes loaded in the app, grepped the directory for MainMenu, and do not see how Tatter\Menus\MainMenu could already be in use.
Reply
#6

You have the class Tatter\Menus\MainMenu in somewhere and it seems it is already autoloaded.

And the namespace in app/Menus/MainMenu.php is incorrect.

// app/Menus/MainMenu.php
namespace Tatter\Menus;
Reply




Theme © iAndrew 2016 - Forum software by © MyBB