Rendering CSS and other archives
Closed this issue · 12 comments
I include a archive HTML direct so:
$r3->any('/produtos/*', function($id){
<!-- code -->
include 'views/produtos.tpl';
});
This archive HTML makes an include in archive CSS
However when I pass a URL with some parameter have that include '../' in path of the CSS archive.
How can solve this?
I'm not sure if I got what's happening. You're getting trouble loading CSS when you use parameters? Can you show us a simplified example of the HTML that causes that issue?
Thanks!
Follows a example:
Index.php
$r3->any('/products', function ($id) {
include 'views/products.tpl';
});
products.tpl
<?php include 'header.tpl'; ?>
<div id="page_products">
</div>
<?php include 'footer.tpl'; ?>
header.tpl The header archive is included in all pages HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="css/default.css" />
<title>Title of the document</title>
</head>
<body>
If I access the page so:
Rendering normally.
If I pass the parameter in URL
http://myproject/products/id=2
Not loading the CSS.
It Is alright include my archives direct so in controller?
Or has a best form of making so? Can be this the problem.
Thanks for the trouble!
Try this:
<link rel="stylesheet" type="text/css" href="/css/default.css" />
I've added a single /
before the css/default.css
path to make it absolute. Also, for a path declared as /produtos/*
, the parameter should be passed as /produtos/2
instead of /produtos/id=2
.
Let me know if that works for you. If don't, I can set up a more complete example!
Not worked. =(
I had already tried to put the path absolute, but not works.
Ok, I changed the way of passed the parameter, continues not works.
For clarity follow my directory structure and instance of Respect\Rest\Router:
app
src
public
css
default.css
images
js
views
header.tpl
index.tpl
products.tpl
bootstrap.php
index.php // This router archive
vendor
$r3 = new Router('/app/public');
Thank you again!
If I am following the problem is that you are not getting the id=2 parameter. If that is correct then maybe fix the URL:
What you supplied:
http://myproject/products/id=2
This does not pass and id parameter instead it will be looking for a router definition:
$r3->any('/products/id');
/** or */
$r3->any('/products/*');
If you change it to:
http://myproject/products/?id=2
The route handler for /products
will be called and you will now be able to retrieve the argument id
in php with:
$id = $_GET['id'];
Does that answer your question and cause happiness all around? =)
Another way to get the $id as per your example.
$r3->any('/products', function ($id) {
include 'views/products.tpl';
});
$r3->any('/products/*', function ($id) {
include 'views/products' . $id . '.tpl';
});
The addition of the * is required to get the function parameter for $id if I recall. Which will be populated wit the string value 2 when called from an example url like this.
http://myproject/products/2
Make sense?
Make sense yes!
The ID parameter, I'm getting normally.
The problem is loading of archive CSS.
I did a test passing the following URL:
http://myproject/public/produtos?id=14
So loads the CSS file!
But if I put a "/ " in URL
Example:
http://myproject/public/produtos/?id=14
or
http://myproject/public/produtos/id=14
or
http://myproject/public/produtos/14
Not loads my file CSS, for this page.
Did I implemented something wrong?
I tried to pass the path absolute how the Alganet spoke, but not works.
Thanks for the trouble!
if you change the route to:
$r3->any('/products/*', function ($id=null) {
if (!is_null($id)) {
/** then $id has the value of the 2nd part of the url 14 for /products/14 */
}
/** this route will also work for /products?id=14 /products/?id=14 actually */
/** even /products/anything-else?id=14 if you get the parameter from the request */
$id = $_REQUEST['id']; // or $_GET or $_POST
// /products/id=14 is not a valid URI I am afraid.
});
Happiness? =)
Still does not load the CSS with this route:
http://myproject/public/produtos/14
Do not wanted using this URL:
http://myproject/public/produtos?id=14
so to load CSS.
Got it?
Ahhh I think I know what the problem is try change the header.tpl referencing the stylesheet as
<link rel="stylesheet" type="text/css" href="/public/css/default.css" />
Then it wil find the stylesheet based on your document root regardless of the url calling the page.
Is that what the problem is?
That! is this the problem.
I put:
<link rel="stylesheet" type="text/css" href="/public/css/default.css" />
but still not load the css.
It only works with absolute path, eg:
<link rel="stylesheet" type="text/css" href="http://www.myapp.com.br/public/css/default.css" />
Thank you again!
a path starting with / is considered an absolute path, I think you have a problem with webserver or perhaps hostname configuration. There is a .htaccess declaration that may address the problem. Let me know if you want me to look into it but if you're happy with that, glad you got it working. =)