1

In order to share CakePhp2 core between many sites I edited CAKE_CORE_INCLUDE_PATH in webroot/index.php to point to the cake directory. That works and I can reach my welcome page.

However, when I attempted to run the Cake Console, I ran into issues with ShellDispatcher.php not defining DS and CORE_PATH because they were only defined if CAKE_CORE_INCLUDE_PATH was NOT defined. Once I defined them out of the if statement which checks if CAKE_CORE_INCLUDE_PATH was not defined, I was good to go. However, I would prefer not to "hack" this file because I want to keep the cake core files clean. Are there any better and cleaner options?

I also had to defined CAKE_CORE_INCLUDE_PATH and use it to set $dispatcher in Console/cake.php which is part of the app of course.

1 Answer 1

1

Yes there are. As a start never define application-wide variables in Webroot/index.php. As a matter of fact you shouldn't be touching this file at all. You can define variables in Config/bootstrap.php.

You were getting the error because when in a Shell you're not firing Webroot/index.php at all - you're actually running PHP's CLI and CakePHP fires off in a different manner. It will go through the bootstrap.php file of course.

You should also use this file to define any global vars, but there is a little bottleneck here when using PHP's $_SERVER variable's web server related members - e.g. HTTP_HOST and the definition: $myHost = $_SERVER['HTTP_HOST'];. If you start a shell Cake will try to set this variable and it will fail throwing an error. This is because as mentioned before PHP will be running in CLI mode when the CakeShell is envoked. There is a way to detect this of course - you can use $_SERVER['SCRIPT_FILENAME'] or $_SERVER['SCRIPT_NAME'] in bootstrap.php to identify if you're in CLI or not. :)

Sign up to request clarification or add additional context in comments.

8 Comments

It was my intention to add standard controllers/views to this app so it has a webapp portion. In that case, isn't it accepted to edit Webroot/inde.php? Can I do both and still separate Cake core from my apps?
No. CAKE_CORE_INCLUDE_PATH is a configuration value and that's what the Config dir is for. Webroot/index.php is the "The Front Controller for handling every request". Using the files under Config is cleaner. You can create a shell.php file in Config and load it in bootstrap.php only if under shell if that is even cleaner for you.
Is Config/bootstrap.php automatically loaded when core is in a different place than the default setup? I ask because as a test I put a die statement at the top of it and I don't seem to be reaching it.
Config/bootstrap.php is in app/ so yes it should be automatically loaded. All CakePHP requests go through it.
As said before you should use bootstrap.php and core.php. I prefer separating application specific configuration from core configuration via these two files - app specific stuff goes into bootstrap.php, Cake core configurations - in core.php. Both will run every time when a request is made or the all is launched in CLI mode.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.