I have a plugin with the name 'Admin' and my methods are like index, add etc. Since I want to apply unique model names and controller names (for ACL implementation), I prefixed all controllers and models with the plugin name (please see cakephp plugin model/controller cache issue with main model/controller). After that, Im facing the following problems. I have a table called users and I am using cakephp2.0.
1) I have to access the url with domain.com/admin/admin_users/index or admin/content/admin_index, instead I want to access by admin/users/index or admin/content/index. How to set this in routes in a general way so that it will be applied to all ?
2) In view, it is showing undefined index User (I have baked the views before). Everything is coming as AdminUser. After setting public $alias = 'User' this issue has been solved. Is this correct ?
3) In controller, I have to change "$this->User->some var/fn " to $this->AdminUser->some var/function is all places. Any way to solve this ?
Anything wrong with plugin name giving as admin (I have not set admin routing) ?
You can safely name your plugin admin, this is by no means a reserved word. However, Cake doesn't take plugin names into account when parsing model / controller names, so AdminUsersController and AdminUser are actually going to be seen as classes belonging to some abstract AdminUser. So to solve all your problems using only Cake magic you'd have to actually name them User and UsersController, and hope these names don't clash. If this is not an option, you can provide some of your own magic to solve these things:
1) How to alias a controller is one of the route configuration examples in the cookbook
2) Yups, if you want to use "User" as a key, that is correct.
3) Not really using only Cake magic, because the $uses variable doesn't support aliasing. You can, however, take advantage of the new lazy-loading of 2.0, by doing something like this in your AdminUsersController:
<?php
public function __get($name) {
// Note, the isset triggers lazy-loading. Check out Controller::__isset()
// if you want to see how that works.
if ($name == 'User' && isset($this->AdminUser)) {
// Assign to admin user here to bypass the next time
return ($this->User = $this->AdminUser);
}
// Delegate to CakePHP's magic getter
return parent::__get($name);
}
Related
Whenever I'm displaying a user's name in my view file, I use:
echo $user['User']['name'];
The users table also has a 'company_name' field. If the user has a company name listed (in most cases, but not all), I would like to display their company name (and if they do not, I want to use the 'name' field). The code would look something like:
if(isset($user['User']['company_name']) && $user['User']['company_name'] != '') {
echo $user['User']['company_name'];
} else {
echo $user['User']['name'];
}
What is the proper way to handle this throughout multiple view files? Should I create a helper for it?
Being that this particular use case is such a small bit of presentation logic it would make more sense to me to build this within the scope of an Element rather than a Helper.
Now if at some point down the road (or now even) you decide that there is more functionality that you would like to extend similar to this presentation logic, perhaps in the form of a UsersHelper that would define various pieces of display logic relevant to the current user, then it would be wise to encapsulate that functionality within a Helper.
Here's a good example to get you started with Elements:
// In src/Template/Element/name_display.ctp
if(isset($user['User']['company_name']) && $user['User']['company_name'] != '') {
echo $user['User']['company_name'];
} else {
echo $user['User']['name'];
}
In your view file:
// Echo this wherever you need the name displayed
echo $this->element('name_display', [
'user' => $user // Sets the $user viewVar in the element
]);
And for sake of completeness, in your controller:
$this->set('user', $user);
In your spare time it would be a good idea just to check out the manual on some of the core helpers that are included with Cake to get an idea of how they are constructed to get a better idea of use cases in which you would want to create a Helper.
2.x Elements Documentation
3.x Elements Documentation
2.x Helpers Documentation
3.x Helpers Documentation
Good luck!
I have two Plugins in cakephp both contains User model. When I want to access the properties of User model it always call first User model. How can I access second User Model?
Not possible - rename your models
Unfortunately that's not possible. Because of the way models are defined, that means you're wanting to load two different classes with the same name in the same namespace (global) which is not possible with PHP - the only solution is to use different classnames.
A standard practice is to name plugin classes prefixed with the plugin name to avoid collisions:
So for example in the foo plugin:
<?php
//App/Plugin/Foo/Model/FooUser.php
class FooUser extends FooAppModel {
}
And in the bar plugin:
<?php
//App/Plugin/Bar/Model/BarUser.php
class BarUser extends BarAppModel {
}
This does lead to slightly more cumbersome usage, but prevents roadblock-problems when trying to access both classes at the same time.
You have to specify the plugin name when you initialize the model classes. So you could call your first one 'User' and your second one 'PluginUser', ie:
$this->User = ClassRegistry::init('PluginOne.User');
$this->PluginUser = ClassRegistry::init('PluginTwo.User');
I'm having trouble wording my problem, so it's been tough to search for an answer. Hopefully you'll know how to help.
I am creating a CakePHP 2.1 Plugin that will interact with a series of its own Models:
- Friend
- Group
- User
Friend and Group are models that are created specifically for the Plugin, and they function within the plugin normally. However, the User model is really just an alias for some other table in the parent app.
So, if "My Awesome Application" decides to use "My Awesome Plugin", it will have to have its own "users" table (though it may called something else). Let's say "My Awesome Application" has a Model called MyUser. "My Awesome Plugin" wants to dynamically tell its internal User model to $useTable = "my_users".
My question is, how do I pass that data to the Plugin? How do I configure "My Awesome Plugin" to understand that User should $useTable "my_users";
As I understand you would like a Model in a PlugIn to use a table that would typically belong to a Model in your Application - by the conventions. Have you tried statically setting:
public $useTable = "my_users";
in the plugin? All plugins usually get initialized when Cake starts up, so all configurations should be loaded then. Why do you need this - it does really restrict you a lot? Will the table being used by the Plugin model change runtime?
The Model class also has some goodies you may find useful:
$this->User->table;
holds the table name for the model - the table that is currently being used that is**.
Also you can set the source table for the Model (inside a Controller) with:
$this->User->setSource('table_name);
** I am not sure if this applies when you use Model::setSource(). It would be interesting to check out what $this->User->table; holds after a Model::setSource() call.
I've figured out a way to accomplish this, but it might not work in all scenarios for all people.
I created a Component in my Plugin, and then I call the Component in my Controller. I pass the name of the users Model through the Component. This way, I can get information about the users Model, and I can set it as the useTable to my Plugin for use in the Plugin.
Of course, this method restricts me to using the Component to utilize the Plugin, but that's probably for the best.
Here's an example of how I did it:
// in the AppController
public $components = array(
'MyPlugin.MyPluginComponent' => array('userModel'=>'UserModelName')
);
// in the Plugin's Component
class MyPluginComponent extends Component {
function initialize($controller) {
//get the base user model
$this->UserModel = ClassRegistry::init($this->settings['userModel']);
//set the useTable for our plugin's user model
$this->PluginUser = ClassRegistry::init('MyPlugin.PluginUser');
//set the useTable value for this model
$this->PleaseUser->setSource($this->UserModel->useTable);
}
That seems to work for me. Hope this helps someone else.
I have a multi-domain site. Depending on the domain the site needs to behave accordingly.
I created a helper called CompanyInfo it has methods such as name(), phone(), email(), etc.
Depending on what domain you are on it returns the correct information.
So for example if I need to display the phone number for a user to call I would use $this->CompanyInfo->phone() and it will display the correct phone number for the user depending on the domain.
Ok, this is all good, but not really relevant. The real issue is, I need this information in more than just the view. Helpers are just for views though. If I want to access this information from a controller I need to create a component to do it.
I really don't want to have a Helper and a Component doing the same thing. I would rather have one class handle it rather than copy and paste logic.
So whats the best way to have a class with methods that can be accessed from the controller or view or even model?
Is it just this kind of static information (name, phonenumber, email, etc) what you need to display? Why not just add them to your configuration in core.php?
Something like
# in core.php
Configuration::write('Company.name', 'Acme Corp.');
Configuration::write('Company.email', 'joe#acme.com');
You can then get this info anywhere you need using
Configuration::read('Company.name');
You can define this variable in your app_controller and then use these variable easily in any of your controller as set these variables from there only.
Call this function in your construct class.
i think that will solve your problem.
You can access model classes from any place this way :
$companyInfoModel = ClassRegistry::init('CompanyInfo');
$phone = $companyInfoModel->phone();
a) you can use libs in cake1.3 for that
b) static model methods which you can pass the content to and which will return the expected value
echo Model::phone($data)
I want to use AuthComponent in CakePHP with a model (and its corresponding table) named Admin ('admins' table in the database). I've used $this->Auth->userModel = 'Admin'; as the documenation says in the BeforeFilter() function of the app_controller, to indicate that I'm not using the deafult model User.
However, either I'm doing it in the wrong place or there's something else that needs to be done, because I can't make the authentication to work (the login function in my admins_controller.php). The rest of functions work well (for example the add function).
If I use the name User for my model with a users table, it works perfectly. How do I fix this problem?
Make sure you call parent::beforeFilter() in your AdminController::beforeFilter().