how to setup environment of 'cake bake' for multiple applications? - cakephp

I am stuck in setting up environment for running cake bake.
In my development machine (using WAMP), I have several applications:
/{wwwroot}
/myapp-1
/myapp-2
/myapp-3
According to some tutorial online, in order to use 'cake' in command prompt, I have to add 'cake/console/' directory as 'PATH' environment variable.
Therefore, I add '{wwwroot}/myapp-1/cake/console' as one of the 'PATH' variables.
But the problem is, when I am going to 'cake bake' some models/controllers for 'myapp-2' or 'myapp-3', the system said something about database table not found and a like.
After some tracing, I suspect that the 'cake bake' is using the 'config' of 'myapp-1' because I am using the 'cake/console' there.
So, what is the proper structure of the environment 'cake bake' multiple applications?
Many thanks!

I would go to your appropriate app folder and directly reference the cake script. It should then use the correct config.
../cake/console/cake bake
there is also a parameter -app that you could use to specify which app you would like to use.

Related

Different branding for same codebase

Im in a project where we will create different sites using the same codebase.
I would like to have a brand style and config for each site which I specify somehow in my build process.
Anyone have an idea of the best way to achieve this ?
I would treat the different sites in much the same way I'd treat different environment (dev, test, prod). If there aren't a lot of changes, just use environment variables on each server where the site will run that define which site it is. Your code can then conditionally do things (e.g. add a class site-x to the body for styling).
You can use something like dotenv to make setting environment vars easier (remember Windows does it differently to *NIX) if you're setting environments in a script. That way you're changing a file rather then actual environment variables when you want to test what a particular site looks like.
If there are many different config items that are different between sites then you can have multiple config files (config-site-one.js, config-site-two.js) and a central config.js file that returns the correct config based on some environment variable like MY_SITE_NAME.
However if you actually want to package up the site to 'send' somewhere (?) then you could run your build command with a flag like webpack blahblahblah --site=site-one.
You can use yargs to get that 'site' variable and use it in your build process however you like.

CakePHP 2.x Acl

I am using CakePHP 2.3.6. In a project, I implemented Acl. I followed the official tutorial given, populated the acos table by cake's shell scripting,used AclExtras plugin, then populated the aros_acos table using "customized" initDb function.
Everything is ok now, the tables are populated successfully. So, I thought I don't need the allow()/deny() functions, which I used before for Authorization. So I deleted these functions from the beforeFilter() functions from the corresponding controllers. But, when I deleted them, I cant access any page(fucnction) in my whole project.
To populate the acos table I used this command :
./Console/cake AclExtras.AclExtras aco_sync
To populate the aros_acos table I customized the initDB() and put it in the Users controller and run it.
All tables are populated, everything is good, but its not working the allow()/deny() functions defined before I implemented ACL.
My question is, if I use Acl, if I have all AROs & ACOs stored in the database, and if I define all permissions in the database, then why do I need those allow()/deny() functions ? And if I need those, then why should I use Acl ? My project was fine without the Acl implementation, with the allow()/deny() functions.
So, what should I do ?
Can anyone help me please ?
Thanks.
Ok,
Finally I got the ACL working. I found that its so easy, I just had to know the shellscrpting. I always knew that its implemented well in CakePHP, I just have to make it working in my project.
First of all, we have to make PHP and CakePHP executable from our shell/command prompt. I did it in Windows(7), will try for Linux & MAC later.
First, if you dont have PHP executable from your command prompt, go to My Computer->Properties->Environment Variables(forgot exact path, but you will get it easily). Then paste this c:\wamp\bin\php\php5.3.13; in the Environment Variable, where php5.3.13 is version-specific.
After that, paste this in the same place : cakephp\lib\Cake\Console. You can copy the cakephp folder to your htdocs/www permanently, for later use.
Now, run this in your command prompt : cake bake all, and follow the instructions. You must have to have a database, configured in your config.php file.
By now, you should be ready with the newly created project. Now follow the CakePHP documentation for ACL.
Remember, which functions/methods you want to be open for all kind of users, allow them explicitly by allow(), in AppController, or in the specific controller.
Suppose, you want pages/index,users/login,users/signUp - these 3 pages/functions to be open for all. So, allow() them in the AppController or in Pages & Users controllers.

CakePHP database file specification

I'm trying to find something about CakePHP configuration, and I'm not having any luck googling and looking around.
I want to make a CakePHP app use "database.local.php" instead of "database.php", but I can't find the line in the app/Config files where the database file is defined as "database.php".
Does anyone know where it is defined so I can change it?
Thanks for your help ahead of time!
If i get it right, you want to have a local database... for development purpose?
If you want to switch the database due to your server-name, you can try to use:
http://www.edwardawebb.com/web-development/cakephp/automatically-choose-database-connections-cakephp
Not quite an answer for you, but I wouldn't change it.
If you're using different files for different environments (dev, staging, production etc) you could use your naming convention, but have your deploy process rename the relevant file to whatever CakePHP's expecting. That is, have:
database.local.php
database.uat.php
database.live.php
or whatever, and just rename to database.php when deploying your code to one of those environments.

Share controllers, models and views in multisite CakePHP 2.3 installation

I'm creating a CakePHP 2.3 advanced installation (several apps -websites- that share one same lib folder where all of cake's core files are located). This works without any problems, I just edit the core.php file in the Config folder for each app so it knows where to find cake's files. The file system looks something like:
[root]
[cake-core-files]
[websites]
[website-1]
[app]
[plugins]
[vendors]
[website-2]
...
[website-N]
These different apps are in fact different in some things (they are different websites) but at the same time there's many things that are common to all of them (for example some models, controllers, functions...). What I would like to do, if possible, is to have those apps also share a bunch of controllers, models, etc so I can put them in one place, instead of replicating them now for each app.
I've seen the concept of vendors and plugins in CakePHP (I actually use plugins in those websites, but from the /app/plugins folder), but I'm not sure if that would work in my case, or how I would set that up. I guess the idea would be to have another folder (for example [shared_objects]) at the same level of [cake-core-files] and [websites], but I don't know how I would have to configure cake to do that or how to call those objects from each app.
Any ideas?
EDIT
Based on the comments/responses below I'm trying to do this using the App:build() function in the bootstrap.php, but I can't get it to work. This is what I've done:
Added a new folder where I want to put the stuff to share between all apps:
[root]
[cake-core-files]
[shared-stuff] --> NEW FOLDER
[Model]
[Config]
[websites]
[website-1]
etc...
Placed the model User.php inside the new folder [shared-stuff/Model]
Added this line in the bootstrap:
App::build(array('Model' => array('/path/to/shared-stuff/Model')));
Deleted the model User.php from its original location [website-1/app/Model]
After this steps, it seems to work, the model User.php is loaded correctly from the [shared-stuff] folder (I've tested that printing App::objects('Model');, the User model is listed). However, it actually doesn't work, when I call that model from the UsersController in the login() function, the login doesn't work (although I don't receive any kind of error, even with debug set to 2).
This model uses a database configuration different from the default one (the users table is located in a different database than the default one). I don't know if this matters.
One thing is for sure, if I leave the same exact User.php model in its original location ( [website-1/app/Model]) it all works fine, including the login, so it's a problem with how I try to configure all this sharing stuff, not the model itself.
Any ideas what am I doing wrong?
I think it could be useful to share some controller/model between multiple websites, and do it without a plugin: Using shared controller/model lets you to overwrite it if needed. It should happen simply copying the controller/model in the website's correct folder and the system should use it instead of the shared one!
EDIT: Wonderful, it works, but i think there is a little error in cake's official documentation: All paths should be terminated with a Directory separator! In the cakephp book there aren't trailing slash. Add it to your App::build path and everything will work perfectly!
You can have plugins in the core Plugins/ dir
[root]
[lib]
[Cake]
[Plugins]
[Available]
[To]
[All]
[website-1] // the 'app' dir -> https://github.com/cakephp/cakephp/tree/master/app
[plugins]
[vendors]
[website-2] // you can have many of them named anything, 'app' is just one.
...
[website-N]
This folder specifically will make the plugins available to any app using the cake lib
EG:
Look at this. Copy app to website-1, repeat till website-n.

How do I use the CakePHP Configure class?

I am trying to use the Configure class in CakePHP, but I'm not sure if I am using it correctly. I have read through the cook book and the API, but I can't seem to do what I want.
I have created a configuration file: app/config/config.php. I can directly edit this file and set variables in there and access them using Configure::read().
Is it possible to update the values of the configuration file from the application itself, i.e., from a controller? I have tried using Configure::write(), but this does not seem to change the value.
app/config/config.php isn't a file that's automatically loaded by Cake. Either move these variables into app/config/bootstrap.php or tell your bootstrap.php file to load your custom file. You could also put your variables in app/config/core.php, but I'd recommend against that. I tend to like leaving that file alone and adding/overwriting values in bootstrap.php.
According to the API, Configure is supposed to be used "for managing runtime configuration information".
You can use its methods to create, read, update and delete (CRUD) configuration variables at runtime. The Configure class is available everywhere in your CakePHP application and therefore CRUD operations performed on its data in any place, including a controller.
If you are looking for persistent storage, you could consider a database (SQL or NoSQL). I would not recommend using a text file, as it raises a lot of security concerns. Even if security is not an issue, a database is propably a more fitting solution.
More on the Configure class in the Cookbook.

Resources