Reputation: 416
At First, I load home page and all images load and it is OK. but when clicked on link and called another action (view action) it's images don't load.
it is my Images path and view.ctp file :
And it is view action :
public function view($id = NULL)
{
$this->layout= false;
$post = $this->Post->read(NULL, $id);
if(isset($this->params['requested'])){
return $post;
}
$this->set('post',$post);
}
Upvotes: 1
Views: 2931
Reputation: 41605
Don't get crazy with PATHS :) Use CakePHP methods and forget about it.
I had the same problem before.
The simple way is storing ALL your images at
/app/webroot/img
Or any subfolder.
And then, using HTML helper to print them:
echo $this->Html->image("name_of_image.jpg", array("alt" => "alternative_text"));
For more information about how to use the image helper look: http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::image
You can apply the same rule for loading CSS, JS files, create links... use CakePHP functions in the correct way and forget about Path problems.
Upvotes: 4
Reputation: 2232
You have a lack of understanding of the relative and absolute paths used in URLs or in general file systems. Let me start somewhere else:
To you problem: There are some things you need to know about paths. There are absolute paths. Those are providing information from the root of your filesystem to the point you want to be: e.g.: /var/www/somefolder/someapp/webroot/index.php (Linux) C:\www\somefolder\someapp\webroot\index.php (Windows)
To make this easier to handle while you are deeep in your filesystem or in an enviroment where you maybe do not even know your absolute path relative paths were "invented". Let's say you are in the directory specified above and you want to access or include the "Config/core.php" of your application: ../Config/core.php (Linux) ..\Config\core.php (Windows - nothing special here except for the slashes to be backslashes)
What this basicly does is: going back to the parent directory of "webroot" which is in this case "/var/www/somefolder/someapp/" (Linux) and for windows "C:\www\somefolder\someapp\". From there we are going forward again to the "Config" folder and accessing the core.php afterwards.
This schema applies to urls as well. E.g. www.example.com/pages/imprint/ . Normally the server would search for a folder "pages" in the webroot of your hosting. In there it would search for a folder "imprint". Afterwards, based on your server config, it would search in that folder for an "index.html" or "index.php" or some other file your adviced the server to use as an index file. Or it could just output the content of the folder.
With the .htaccess file in your "[application]/webroot/" folder this behavior gets redirected to the index.php file of CakePHP. Cake is analyzing the url and checks if there are any valid controller actions or routes for it and afterwards run the appropriate code for it. But! this rule applies not on actual files or folders in the webroot directory. (Take a look at the htaccess file in the webroot folder and google for the commands used in there). This is why css files can be delivered without any problems if you are using the right syntax. Since CakePHP uses the folder url syntax for its urls giving relative urls is a problem because they are only right for one specific page most of the time. E.g. for /pages/imprint ../img/blabla.gif would go back to webroot and show the specific picture, because the url to use would be "webroot/img/blabla.gif". But in the same application /products/view/1 the path ../img/blabla.gif would end up in "webroot/products/img/blabla.gif" which is not an actual file, cakephp would try to process it and it would fail because the url is not valid.
I hope this helps out.
Checkout those paths a little more. Understanding those is essential for programming and it helped me a lot back then ;)
Greetings func0der
Upvotes: 3