Giter VIP home page Giter VIP logo

Comments (22)

piotr-cz avatar piotr-cz commented on May 30, 2024

Thinking again about it, bulletproof solution would be a function generating an absolute path: basePath + $path
This is how links are generated in Joomla CMS.

from twig-view.

nicokaag avatar nicokaag commented on May 30, 2024

My suggestion would be:

Having a path_for function which which returns the "absolute" path, and takes an optional parameter like relativePath, that returns the path as exactly defined in the routing.
The reason for returning the absolute path by default is that it's by far the most used use-case. In your case it's that your project runs in a sub-directory, but also when using index.php, and you have no pretty url rewriting, it will always break.

And another function url_for, pretty much doing the same as path_for, but with the url in front of it.

from twig-view.

piotr-cz avatar piotr-cz commented on May 30, 2024

Reading the slimphp/Slim#1326, I see that the Uri::getBaseUrl() method have been created to solve this problem in Slim.

I'd just need something slightly similar for views.

from twig-view.

silentworks avatar silentworks commented on May 30, 2024

@piotr-cz that is also available in the view now.

{{ base_url() }}

So you could do:

{{ base_url() }}{{ path_for('my_route_name') }}

from twig-view.

piotr-cz avatar piotr-cz commented on May 30, 2024

@silentworks
I know, but why having two functions, each of very little use on it's own in views instead of one?

I'd use base_url in <base href /> tag. In links to assets I'd rather use a path relative to document root, but it's a matter of preference.

I can't think of scenario when I'd use path_for by itself in current state at all.

from twig-view.

silentworks avatar silentworks commented on May 30, 2024

I think this is an improvement, before we only had url_for which is now path_for, but now we also have base_url for those who want to make use of it.

from twig-view.

akrabat avatar akrabat commented on May 30, 2024

I use path_for by itself in my project with no problems.

from twig-view.

nicokaag avatar nicokaag commented on May 30, 2024

For me it kind of feels wrong that path_for breaks with a basic project setup, where you don't use url-rewriting. The example in the readme will only work if you use url-rewriting to get rid of the index.php.

But I think that that is more an issue for the Slim project itself, and not this one(?).

from twig-view.

piotr-cz avatar piotr-cz commented on May 30, 2024

@akrabat Did you try putting a project in a subfolder?

My structure is:

http://localhost:8000
  /clients
    /foo
    /bar
  /personal
    /helloWorld

When I use path_for in the /personal/hellloWolrd project, it generates strings like /contact so
URL is resolved to http://localhost:8000/helloWorld/contact, which makes apache to return 404 error.

from twig-view.

silentworks avatar silentworks commented on May 30, 2024

@piotr-cz Is this not solved by adding base_url before path_for?

from twig-view.

piotr-cz avatar piotr-cz commented on May 30, 2024

@silentworks It is, but what's the use case for path_for at all?

from twig-view.

akrabat avatar akrabat commented on May 30, 2024

I don't run my projects like that. I was responding to your assertion "I can't think of scenario when I'd use path_for by itself in current state at all." to point out that it is useful for my use-case and works exactly as I expect.

There's a reasonable argument for {{ base_path_for('routename', {foo: "bar:}) }}, which if someone wants to PR, I'm sure @silentworks or myself would review and merge.

I don't want path_for to change as it's a direct mapping to the router's pathFor and it would be very confusing to have pathFor and path_for do different things.

from twig-view.

silentworks avatar silentworks commented on May 30, 2024

@piotr-cz the same use case url_for had before, most of my project's are not installed in subdirectories and path_for is sufficient for me. I don't actually want full url at all times as I only want the path.

from twig-view.

piotr-cz avatar piotr-cz commented on May 30, 2024

I'll prepare a pull request for base_path_for then, this makes sense.

Probably this will solve the issue @nicokaag is having.

Placing projects in subfolders is the way I run multiple projects on one vagrant box, but it may also be helpful for plugging api created in Slim into a /api sufolder witin existing projects.

from twig-view.

akrabat avatar akrabat commented on May 30, 2024

Thanks. I appreciate that its a valid use-case, along with the index.php one.

from twig-view.

piotr-cz avatar piotr-cz commented on May 30, 2024

@nicokaag Can you please check if #27 solves your issue?

from twig-view.

nicokaag avatar nicokaag commented on May 30, 2024

@piotr-cz it does work.

I do still wonder if the Slim\Router pathFor method shouldn't solve this.
In Slim 2, the urlFor method already solves this.

Is there a separate issue list for Slim 3 where these kind of things can be discussed?

from twig-view.

silentworks avatar silentworks commented on May 30, 2024

@nicokaag I cannot see how urlFor in Slim 2 solved this, pathFor is the same as urlFor, just the name change.

from twig-view.

nicokaag avatar nicokaag commented on May 30, 2024

Ah, I see now that Slim 2 has an extra method in the Slim\Slim class, also called urlFor, that solves the issue, so that you can do $app->urlFor('name', $params);. And that function looks like:

public function urlFor($name, $params = array())
{
    return $this->request->getRootUri() . $this->router->urlFor($name, $params);
}

Not sure if that is actually something that you'd want.

from twig-view.

piotr-cz avatar piotr-cz commented on May 30, 2024

This could be helpful in the Slim too, for example for creating redirects.

But it should not be called pathFor, because it implies using paths. Maybe it should be rather called urlFor (full url) or basePathFor (absolute path) what do you think?

from twig-view.

akrabat avatar akrabat commented on May 30, 2024

The Router's pathFor is now base path aware and returns an absolute URL relative to the domain name.

See slimphp/Slim#1443

from twig-view.

piotr-cz avatar piotr-cz commented on May 30, 2024

great.

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.