Comments (22)
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.
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.
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.
@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.
@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.
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.
I use path_for
by itself in my project with no problems.
from twig-view.
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.
@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.
@piotr-cz Is this not solved by adding base_url
before path_for
?
from twig-view.
@silentworks It is, but what's the use case for path_for
at all?
from twig-view.
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.
@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.
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.
Thanks. I appreciate that its a valid use-case, along with the index.php one.
from twig-view.
@nicokaag Can you please check if #27 solves your issue?
from twig-view.
@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.
@nicokaag I cannot see how urlFor in Slim 2 solved this, pathFor is the same as urlFor, just the name change.
from twig-view.
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.
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.
The Router's pathFor
is now base path aware and returns an absolute URL relative to the domain name.
from twig-view.
great.
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
- is_current_url() doesn't take basepath into consideration HOT 20
- 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 3
- Error using Twig with Slim 4 & Container HOT 3
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.