Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the root directory of CakePHP?

I've got a view with a formular (which isn't created with the form helper). For example, I have CakePHP installed in a folder called 'myfolder1'.

So my formular starts with that line:

<form id="ctrlAddForm" method="post" action="/myfolder1/ctrl/add" accept-charset="utf-8">

But if I rename the root folder, I have to edit all formulars (2 yet). Is there any constant or function which represents the root directory / name?

I've read about the constant ROOT. Should I use that variable?

Solution

  1. You can define a constant ROOT_NAME in app\webroot\index.php with basename(ROOT).
  2. You can use the HTML Helper function url(). For example in the action attribute of the form tag:

    $this->Html->url(array('controller' => 'ctrl', 'action' => 'add'));

like image 439
ComFreek Avatar asked Jun 29 '11 14:06

ComFreek


4 Answers

Well, you could try this normal ol' PHP function: basename

basename(APP);

Since that doesn't work I actually opened up my own Cake install. Try this:

basename(dirname(APP));
like image 138
Charles Sprayberry Avatar answered Nov 07 '22 21:11

Charles Sprayberry


I had to get the URL of the CakePHP document root to use in Javascript, and I was able to set it up using the solution above:

<script language="Javascript">
$(document).ready(function() {
         docRoot = '<?php echo $this->Html->url('/'); ?>';
}
</script>

and that piece of code - $this->Html->url('/') - works perfectly in getting the root directory/URL for CakePHP.

like image 31
Suman Avatar answered Nov 07 '22 22:11

Suman


This page has a list of the constants that can be used, http://book.cakephp.org/view/1141/Core-Definition-Constants

like image 1
Frank Martin Avatar answered Nov 07 '22 22:11

Frank Martin


YOu can check the routes /var/www/yourprojectname/cake/conf/paths.php

you can get all constant variable are being used through the CAKEPHP.

I hope that helps.

Regards, Archit.

like image 1
Archit Patel Avatar answered Nov 07 '22 21:11

Archit Patel