zendframework3-服务执行两次



我创建了一个个性化服务来确定用户的语言。我在Moduel.php的Module类中调用我的服务,如下所示:

$languageService = $sm->get("LanguageService");
$languageService->setLanguage();

服务的配置可以在global.php配置文件以及下面的代码中找到。

'service_manager' => [
'factories' => [
ApplicationServiceLanguageService::class =>  ApplicationServiceFactoryLanguageServiceFactory::class
],
'aliases'=>[
'LanguageService'=>ApplicationServiceLanguageService::class
]
],

使用xdebug的问题代码执行了两次(构造函数和方法(

我注意到代码实际上执行了两次。第一次请求的url是/因此是我的索引。第二个调用是url/css/bootstrap-select.css.map。我认为这是对插件的内部调用。我认为这种行为不正确。

行为是正确的。您的服务器出现问题。我不知道nginx类型的修复方法,但对于Apache,使用.htaccess,您应该立即添加一些代码,为公共目录中的文件提供服务,而不是遍历应用程序。

默认情况下,Zend Skeleton应用程序在public/目录(源(中附带以下.htaccess文件内容:

RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
# The following rewrites all other queries to index.php. The 
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting or installed the project in a subdirectory,
# the base path will be prepended to allow proper resolution of
# the index.php file; it will work in non-aliased environments
# as well, providing a safe, one-size fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}/index.php [L]

项目根目录没有附带.htaccess文件。如果您正确地将服务器映射为使用项目public/目录作为项目访问点,则不需要它


根据评论讨论,我假设某些配置不正确,并将/css/*请求定向到项目根。为了确保这些请求不会进入项目,我在项目根目录中有以下.htaccess

RewriteEngine On
# If URL to the application is http://foo.com/path/to/ZendSkeletonApplication/
# the set the base to /path/to/ZendSkeletonApplication/
RewriteBase /
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Below is ZF2 default
RewriteRule ^.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ public/index.php [NC,L]

不是很漂亮,但它完成了任务。

最新更新