I am using following code in the cakephp project in bootstrap to define constants like webroot path. Is it right
$_ROOTPATH = strtolower('http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
$URLPART = explode('cake',$_ROOTPATH);
define('ROOTPATH',$URLPART[0].'cake/');
define('RESOURCEBASEPATH',$URLPART[0].'resources/');
define('DOCUMENTROOTPATH',substr($_SERVER['SCRIPT_FILENAME'],0,-9));
- ROOTPATH in place of $this->webroot .
- DOCUMENTROOTPATH pointing to actual to piont to the actual path as in
/var/www/
What do you say about this. Any note/suggestion is welcome.
Regarding file paths, have you looked at the existing core constants? You will probably find most of the file path constants you need are already defined there.
For example, your
DOCUMENTROOTPATHis exactly the same as (I believe) theWWW_ROOTconstant.As for URLs, CakePHP’s
Router::url()takes care of handling all URLs in a consistent manner (eg. URLs output byController::redirect(),FormHelper,HtmlHelper, etc.)You can also call
Router::url()statically wherever you need to create URLs:Unless I am missing something, I would just use the above instead of littering your application with the likes of
ROOTPATHandRESOURCEBASEPATH. For example the following approaches are equivalent: