简体   繁体   中英

CakePHP 1.3 app Model functions

I am trying to create a function that changes the locale of a site based on the domain extension but when I try and set the value of a Variable in AppModel as a function I get an error. I am not sure what I am doing wrong.

FYI: $_SERVER['HTTP_HOST'] = '.de';

class AppModel extends Model {

//var $locale = 'de_de'; // Example of what I need

var $locale = $this->getLocale();

    function getLocale() {

        $domain = explode('.', $_SERVER['HTTP_HOST']); 

        if ($domain[1] == 'de') {

            return 'de_de';

        } else {

        return 'en_gb';

        }

    }

}

Error Returned:

Parse error: syntax error, unexpected T_VARIABLE in /var/www/devsite/v1/site/app/app_model.php on line 7 Call Stack: 0.0002 671648 1. {main}() 
/var/www/devsite/v1/site/app/webroot/index.php:0 0.0255 5883776 2. Dispatcher->dispatch() /var/www/devsite/v1/site/app/webroot/index.php:83 0.0264 5949592 3. 
Dispatcher->__getController() /var/www/devsite/v1/site/cake/dispatcher.php:116 0.0264 5949672 4. Dispatcher->__loadController() 
/var/www/devsite/v1/site/cake/dispatcher.php:385 0.0265 5951760 5. App->import() /var/www/devsite/v1/site/cake/dispatcher.php:413 0.0265 5953552 6. 
App->__settings() /var/www/devsite/v1/site/cake/libs/configure.php:916 0.0265 5954000 7. App->import() 
/var/www/devsite/v1/site/cake/libs/configure.php:1171 0.0265 5957624 8. App->__find() /var/www/devsite/v1/site/cake/libs/configure.php:955 0.0268 5984264 9. 
App->__load() /var/www/devsite/v1/site/cake/libs/configure.php:1019 0.0269 6047416 10. require('/var/www/devsite/v1/site/app/app_controller.php') 
/var/www/devsite/v1/site/cake/libs/configure.php:1060 0.0269 6047560 11. App->import() /var/www/devsite/v1/site/app/app_controller.php:8 0.0270 6051456 12. 
App->__find() /var/www/devsite/v1/site/cake/libs/configure.php:955 0.0270 6052240 13. App->__load() /var/www/devsite/v1/site/cake/libs/configure.php:1036 
0.0272 6164128 14. require('/var/www/devsite/v1/site/cake/libs/sanitize.php') /var/www/devsite/v1/site/cake/libs/configure.php:1060 0.0272 6164416 15. 
App->import() /var/www/devsite/v1/site/cake/libs/sanitize.php:2 0.0273 6165128 16. App->__settings() /var/www/devsite/v1/site/cake/libs/configure.php:916 0.0337 8579264 17. 
App->import() /var/www/devsite/v1/site/cake/libs/configure.php:1149 0.0337 8582864 18. App->__find() /var/www/devsite/v1/site/cake/libs/configure.php:955 0.0338 8583952 19. 
App->__load() /var/www/devsite/v1/site/cake/libs/configure.php:1019

Thanks in advance.

the error message says it pretty clearly: invalid php

you still have to code valid PHP (even if it is cakePHP)! using a proper IDE will outline the error right away:

var $uses ('App');

should be

public $uses = array('App');

as documented!

if you are still on PHP4 you would need "var" instead of "public"

you should really start reading basic php books because you seem to lack the basic stuff. you can also not use dynamic methods in the class declaration:

 var $locale = $this->getLocale();

you need to use the constructor for this:

public function __construct($id = false, $table = null, $ds = null) {
    parent::__construct($id, $table, $ds);

    $this->locale = $this->getLocale();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM