Comments (21)
Why?
The problem occurs while using slim in the sub-directory.
While running slim in a sub-directory the $request->getUri()->getPath()
will returns the absolute path.
// create app
$app = AppFactory::create();
$app->setBasePath('/project/slim4');
// twig
$twig = Twig::create('.');
// middleware
$app->add(TwigMiddleware::create($app, $twig))
Let's check the path (in middleware or controller/action)
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args = []) : ResponseInterface
{
exit($request->getUri()->getPath()); # /project/slim4
}
The constructor of TwigRuntimeExtension
accepts 3 parameters routeParser, uri and basePath.
In isCurrentUrl
and getCurrenturl
methods:
$currentUrl = $this->basePath.$this->uri->getPath();
$this->basePath
returns /project/slim4
$this->uri->getPath()
returns /projects/slim4/
so $currentUrl
returns /project/slim4/project/slim4/
😒
Solution!
This method can be applied to make it work without making any changes to Twig-View
// create app
$app = AppFactory::create();
$app->setBasePath('/project/slim4');
// twig
$twig = Twig::create('.');
// middleware
$app->add(new TwigMiddleware($twig, $app->getRouteCollector()->getRouteParser()));
from twig-view.
Hello @l0gicgate,
I think I've run into the same issue, and made a simple test case. Within my Apache DocumentRoot, I created a folder named testcase
and added the following index.php
file:
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
require __DIR__ . '/vendor/autoload.php';
$app = AppFactory::create();
$app->setBasePath('/testcase');
$twig = Twig::create('.');
$app->add(TwigMiddleware::create($app, $twig));
$app->get('/', function (Request $request, Response $response, $args) {
$view = Twig::fromRequest($request);
$response->getBody()->write($view->fetchFromString('{{ current_url() }}'));
return $response;
});
$app->run();
The following .htaccess
file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
And the following composer.json
file:
{
"require": {
"slim/slim": "^4.6",
"slim/twig-view": "^3.1",
"slim/psr7": "^1.2"
}
}
After composer install
if I access the directory in the web browser, the output from the current_url()
is /testcase/testcase/
. I haven't investigated further, but it does seem like base path should not be prepended to the path from the URI.
from twig-view.
Any news on this issue ? Because the solution proposed by iRaziul not work for me. I use TwigWebpackExtension and it break all links to js and css. I need to set the basePath.
from twig-view.
Are you instantiating the TwigMiddleware
with the same basePath
as you have set in Slim?
from twig-view.
I'm not sure actually, i remember implementing it being a real pain.
from twig-view.
Are you sure this gets called:
https://github.com/MrBrax/TwitchAutomator/blob/947956114a9002ebcbb909d1efc8b191b249ac9b/public/index.php#L57
Because if you look in the TwigMiddleware
it gets the basePath
from the app:
https://github.com/slimphp/Twig-View/blob/3.x/src/TwigMiddleware.php#L71
from twig-view.
I didn't know that was possible, nor what that thing does. Gonna have to research more.
from twig-view.
Are you sure this gets called:
https://github.com/MrBrax/TwitchAutomator/blob/947956114a9002ebcbb909d1efc8b191b249ac9b/public/index.php#L57
Yes, the basepath works everywhere else, in the base_path() function too
from twig-view.
You should try and do some debugging by putting a var_dump($currentUrl)
at this line:
https://github.com/slimphp/Twig-View/blob/3.x/src/TwigRuntimeExtension.php#L80
As you can see, the base path is taken into consideration there.
from twig-view.
That tells me that the basepath is appended twice.
<a href="{{ url_for('about') }}"
becomes /path/path/about
from twig-view.
That doesn't give me any insight on what's going on. You will need to debug what's happening in TwigRuntimeExtension
and var_dump($this->uri)
and var_dump($this->basePath)
.
Add those statements in the vendor source files where those files are located.
from twig-view.
$currentUrl = "/path/path/dashboard"
$this->uri = object(Slim\Psr7\Uri)#160 (8) { ["scheme":protected]=> string(4) "http" ["user":protected]=> string(0) "" ["password":protected]=> string(0) "" ["host":protected]=> string(17) "example.com" ["port":protected]=> int(14184) ["path":protected]=> string(15) "/path/dashboard" ["query":protected]=> string(10) "/dashboard" ["fragment":protected]=> string(0) "" }
$this->basePath = "/path"
from twig-view.
It seems that something is modifying the request URI and adding the basepath to it and it shouldn’t.
from twig-view.
nginx maybe? setting nginx up with subfolders took me days of trial and error, almost nothing worked. maybe there's something in there
from twig-view.
I'll keep it broken though, too much weird stuff to debug. Thanks though.
from twig-view.
I have the same issue. The base path is applied twice.
from twig-view.
I think there is an issue with double invocation when resolving the middleware perhaps.. It'd be great to get a failing test case so we can fix this.
from twig-view.
Funny how I've moved on to a SPA since then, but i hope this gets resolved.
from twig-view.
I have the same issue, configured a nginx as a reverse proxy to my application:
https://domain.com/myapp -> http://internal_ip/
In TwigRuntimeExtension::isCurrentUrl:
- $this->basePath: /myapp
- $this->uri->getPath(): /myapp/request (already have the basepath)
The solution proposed by @iRaziul worked for me.
from twig-view.
Any news on this issue ? Because the solution proposed by iRaziul not work for me. I use TwigWebpackExtension and it break all links to js and css. I need to set the basePath.
I have the exact same issue, is there any will to fix it ?
from twig-view.
Related Issues (20)
- Question - Twig 3 Support HOT 10
- Slim\Views\Twig::create does not exist HOT 3
- Add support for Twig 3 in 3.x branch HOT 1
- Slim 4 Compatibility In The Pipeline? HOT 1
- Rendering no space HOT 1
- Fix incorrect declaration HOT 3
- Version on Readme HOT 1
- Twig could not be found in the server request attributes using the key "view" HOT 5
- ,
- How to get args for is_current_url()? HOT 2
- Using view outside a request/response cycle does not load the Slim/Views/TwigExtension HOT 2
- Trying to use TwigExtension template functions returns an error HOT 1
- Uncaught Twig\Error\SyntaxError: Unknown "defined" test. HOT 3
- Support PHP 8.0
- twig->fetch() outputs to browser rather than return string HOT 8
- 3.3.0 Release date HOT 3
- `composer require slim/twig-view:^3.0` handle PHP Version Problem HOT 1
- Rendering a template from a middleware HOT 4
- Error using Twig with Slim 4 & Container HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from twig-view.