'No base path provided' Error in Mezzio Skeleton

Hi,

When I try to use <?= $this->basePath();?> in a template in mezzio, I get the ‘No base path provided’ error:

I am using laminas view, and this is a basic install of the mezzio skeleton.

Should this work?
Installation steps:

Composer.json

{
	"name" : "mezzio/mezzio-skeleton",
	"description" : "Laminas mezzio skeleton. Begin developing PSR-15 middleware applications in seconds!",
	"type" : "project",
	"license" : "BSD-3-Clause",
	"keywords" : [
		"laminas",
		"mezzio",
		"skeleton",
		"middleware",
		"psr",
		"psr-7",
		"psr-11",
		"psr-15"
	],
	"homepage" : "https://mezzio.dev",
	"support" : {
		"docs" : "https://docs.mezzio.dev/mezzio/",
		"issues" : "https://github.com/mezzio/mezzio-skeleton/issues",
		"source" : "https://github.com/mezzio/mezzio-skeleton",
		"rss" : "https://github.com/mezzio/mezzio-skeleton/releases.atom",
		"chat" : "https://laminas.dev/chat",
		"forum" : "https://discourse.laminas.dev"
	},
	"config" : {
		"sort-packages" : true
	},
	"extra" : {
		"laminas" : {
			"component-whitelist" : [
				"mezzio/mezzio",
				"mezzio/mezzio-helpers",
				"mezzio/mezzio-router",
				"laminas/laminas-httphandlerrunner",
				"mezzio/mezzio-fastroute",
				"mezzio/mezzio-laminasviewrenderer"
			]
		}
	},
	"require" : {
		"php" : "^7.3 || ~8.0.0 || ~8.1.0",
		"composer/package-versions-deprecated" : "^1.10.99",
		"laminas/laminas-component-installer" : "^2.5.0",
		"laminas/laminas-config-aggregator" : "^1.5.0",
		"laminas/laminas-diactoros" : "^2.6.0",
		"laminas/laminas-stdlib" : "^3.3.1",
		"laminas/laminas-zendframework-bridge" : "^1.2.0",
		"mezzio/mezzio" : "^3.5.0",
		"mezzio/mezzio-helpers" : "^5.6.0",
		"laminas/laminas-servicemanager" : "^3.4",
		"mezzio/mezzio-fastroute" : "^3.0.3",
		"mezzio/mezzio-laminasviewrenderer" : "^2.2"
	},
	"require-dev" : {
		"laminas/laminas-development-mode" : "^3.3.0",
		"mezzio/mezzio-tooling" : "^1.4.0",
		"phpspec/prophecy" : "^1.10.3",
		"phpspec/prophecy-phpunit" : "^2.0",
		"phpunit/phpunit" : "^9.4.1",
		"roave/security-advisories" : "dev-master",
		"filp/whoops" : "^2.7.1"
	},
	"autoload" : {
		"psr-4" : {
			"App\\" : "src/App/src/"
		}
	},
	"autoload-dev" : {
		"psr-4" : {
			"AppTest\\" : "test/AppTest/"
		}
	},
	"scripts" : {
		"post-create-project-cmd" : "@development-enable",
		"development-disable" : "laminas-development-mode disable",
		"development-enable" : "laminas-development-mode enable",
		"development-status" : "laminas-development-mode status",
		"mezzio" : "mezzio --ansi",
		"check" : [
			"@cs-check",
			"@test"
		],
		"clear-config-cache" : "php bin/clear-config-cache.php",
		"cs-check" : "phpcs",
		"cs-fix" : "phpcbf",
		"serve" : "php -S 0.0.0.0:8080 -t public/",
		"test" : "phpunit --colors=always",
		"test-coverage" : "phpunit --colors=always --coverage-clover clover.xml"
	}
}

The helper is not automatically configured in a Mezzio application, only in a laminas-mvc based application:

If you’re running a laminas-mvc application, basePath() will point to the public folder of the application’s root.

See: Redirecting...

There are different options to solve the problem:

  • if you already use the basePath support of the Mezzio helpers, then you can get the base path from the url helper and set it manually to the basePath helper
  • create a factory for the basePath helper
  • create a feature request on the repository of mezzio-laminasviewrenderer
    (I think a good idea is to create a factory for the basePath helper in laminas-view, which is then used in mezzio-laminasviewrenderer.)

Thanks for the feedback.
I had implemented <?php echo $this->url('home')?> along with the los/basepath middleware which is probably a terrible way of doing it, but it meets my needs so far,

I’ll look into the factory for the basePath helper. It should be cleaner for the application overall.

For what its worth, this is my factory that sets basePath.
As I am using los/basepath , I use it to set the basePath in the factory

<?php
namespace App\Factory;

use Interop\Container\ContainerInterface;
use Laminas\View\Helper\BasePath;

class BasePathFactory
{
    public function __invoke(ContainerInterface $container, $name, array $options = null): BasePath
    {
        $helper = new \Laminas\View\Helper\BasePath();
        $basePath = $container->get('config')['los_basepath'];
        
        $helper->setBasePath($basePath);
        
        return $helper;
    }
}

And in the dependencies.global.php, include the factory in the view_helpers:

'view_helpers' => [
        'factories' => [
            \Laminas\View\Helper\BasePath::class => \App\Factory\BasePathFactory::class
        ]
    ]

Do you use the los/basepath middleware only for the basePath view helper or do you really run your application in a subdirectory?
If your application is not in a subdirectory, try to omit the los/basepath middleware and get the base path from the Mezzio UrlHelper.

The entire application, while in development, is run in a sub-directory, so there was a need for los/basepath