# Cakephp and webroot

**URL:** <https://discourse.cakephp.org/t/cakephp-and-webroot/1270>\
**Category:** Uncategorized\
**Created:** [October 2, 2016, 3:56pm UTC](https://discourse.cakephp.org/t/cakephp-and-webroot/1270 "2016-10-02T15:56:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [October 2, 2016, 3:56pm UTC](https://discourse.cakephp.org/t/cakephp-and-webroot/1270/1 "2016-10-02T15:56:22Z")

</div>

I am mainly a database person, not framework. I am trying to figure how exactly cake handles the “not showing” of webroot folder. For example I have some images under webroot in a subfolder named upload. And under upload more folders with images. My testsite name is just called cakephp for now, will change later. Now in the browser if I right click image in firefox and view image it shows

```auto
http://mysite/cakephp/upload/imgdogs/rover.jpg

```

But rover.jpg is at public\_html\cakephp\webroot\upload\imgdogs\rover.jpg  
How is cakephp doing this, (not showing /webroot in url) I am trying to learn how it’s done…

To add I made a constant in paths .php

```auto
define('DIR', '/cakephp/');

```

so in view I can simply

```auto
$linev = $linev . '<img src="' . DIR . 'upload/imgdogs/' . $row->dogpic . '" alt="" class="image"></a></td>';

```

But still doesn’t help explain how cake bypasses showing webroot, thanks

Edit: I noticed that

```auto
$linev = '<td class="imgtd"><a href="' . DIR . 'dog/view?dogid=' . $row->dogid . '">';

```

links to correct results which is

```auto
http://localhost/cakephp/dog/view?dogid=216

```

BUT testing and taking out DIR temp substitute “”

```auto
$linev = '<td class="imgtd"><a href="' . "" . 'dog/view?dogid=' . $row->dogid . '">';

```

Doesn’t work I get this

```auto
http://localhost/cakephp/dog/dog/view?dogid=216

```

dog/ twice. But this

```auto
$linev = '<td class="imgtd"><a href="' . "" . 'view?dogid=' . $row->dogid . '">';

```

works, so here taking DIR out and dog/ automatically knew I needed dog/

You see the confusion, how does cake know I need dog/ added? the route to the view is

```auto
$routes->connect('/dog/view/', ['controller' => 'Dogs', 'action' => 'view']);

```

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [October 2, 2016, 6:28pm UTC](https://discourse.cakephp.org/t/cakephp-and-webroot/1270/2 "2016-10-02T18:28:24Z")

</div>

The not showing webroot trick is done by the [.htaccess in the main directory](https://github.com/cakephp/app/blob/master/.htaccess) and in the [webroot directory](https://github.com/cakephp/app/blob/master/webroot/.htaccess) (that only works with apache, for others [you should configure](http://book.cakephp.org/3.0/en/installation.html#url-rewriting) in its own way)

You don’t need to redefine the directories. That’s when you are not using the default app folder structure (like having the cakephp core in another directory than the src/config)

And last, for links you should [generate urls](http://book.cakephp.org/3.0/en/views/helpers/url.html) with the Helper
