Giter Club home page Giter Club logo

Comments (21)

iRaziul avatar iRaziul commented on September 27, 2024 5

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.

kloor avatar kloor commented on September 27, 2024 1

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.

diodo avatar diodo commented on September 27, 2024 1

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.

l0gicgate avatar l0gicgate commented on September 27, 2024

Are you instantiating the TwigMiddleware with the same basePath as you have set in Slim?

from twig-view.

MrBrax avatar MrBrax commented on September 27, 2024

I'm not sure actually, i remember implementing it being a real pain.

Current setup

from twig-view.

l0gicgate avatar l0gicgate commented on September 27, 2024

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.

MrBrax avatar MrBrax commented on September 27, 2024

I didn't know that was possible, nor what that thing does. Gonna have to research more.

from twig-view.

MrBrax avatar MrBrax commented on September 27, 2024

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.

l0gicgate avatar l0gicgate commented on September 27, 2024

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.

MrBrax avatar MrBrax commented on September 27, 2024

That tells me that the basepath is appended twice.

<a href="{{ url_for('about') }}" becomes /path/path/about

from twig-view.

l0gicgate avatar l0gicgate commented on September 27, 2024

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.

MrBrax avatar MrBrax commented on September 27, 2024

$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.

l0gicgate avatar l0gicgate commented on September 27, 2024

It seems that something is modifying the request URI and adding the basepath to it and it shouldn’t.

from twig-view.

MrBrax avatar MrBrax commented on September 27, 2024

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.

MrBrax avatar MrBrax commented on September 27, 2024

I'll keep it broken though, too much weird stuff to debug. Thanks though.

from twig-view.

bYemma avatar bYemma commented on September 27, 2024

I have the same issue. The base path is applied twice.

from twig-view.

l0gicgate avatar l0gicgate commented on September 27, 2024

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.

MrBrax avatar MrBrax commented on September 27, 2024

Funny how I've moved on to a SPA since then, but i hope this gets resolved.

from twig-view.

paulocanedo avatar paulocanedo commented on September 27, 2024

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.

dfranco avatar dfranco commented on September 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.