CakeDC's Users plugin action overriding - cakephp

I have installed CakeDC's Users plugin and I have loaded all the required files in bootstrap.php, as shown below:
CakePlugin::load('Users'); // Loads the Users plugin
CakePlugin::load('Search'); // Loads the Search plugin
CakePlugin::load('Utils'); // Loads the Utils plugin
CakePlugin::load('Users', array('routes' => true));
I would like to override the add() action of the UsersController of the plugin, so I created the AppUsersController.php file in app/Controller, as instructed here:
https://github.com/CakeDC/users#extending-the-controller
Then I created an add() action inside the AppUsersController.php, with an empty body, but the original action was not overridden. What am I doing wrong?

Don't create an empty add() on the AppUsersController, because it still load the parent function. Try to copy paste add() from UsersController, and then paste into AppUsersController, then override it

Related

Creating static home page for CakePHP3

I am working on a CakePHP3 project. I want a static homepage that will be loaded on www.mysite.com.
For this I have created a PagesController which will handle all the static pages in the website like about,contact,etc.
I am having display.ctp view in Template/Pages/display.ctp to load on www.mysite.com.
But, for testing (routes are not configured yet), I'm using www.mysite.com/pages and www.mysite.com/pages/display to show the view but it gives error as
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mysite.pages' doesn't exist
Do I need to create Table for this ?
It is much, much easier than that
For this I have created a PagesController
There is already a pages controller for serving static content, and a
static template for the home page, there is no need to create/overwrite the default pages controller, to replace it with the same (or less) functionality. This is also mentioned in the documentation
The steps to modify static pages (with default routes) are:
edit src/Template/Pages/home.ctp - look at the url /
create/edit src/Template/Pages/something.ctp - look at the url /pages/something
The error means that the application is looking for a model named Page. To tell the application that your controller does not refer to any model you have to use something like bellow. Also add the proper action. www.mysite.com/pages/display means in controller "pages" call action "display".
class MyController extends AppController {
var $uses = false;
public function display {}
}

cakePHP custom view folder

I have a UsersController and index() action so by default cakePHP will look for the index.ctp in Users view folder. What i would like to do is to create a separate folder called Partials and have the controller look for the view inside the folder instead. Please help as I just started my journey in cake. Thanks
You can define the path to your view by $this->viewPath in your controller action. E.g. for the following folder strucuture:
... you can access the various templates by the following statements:
function index()
{
// by code conventions this action automatically matches /Home/index.ctp
$this->viewPath = "/Partials/"; // matches /Partials/index.ctp
$this->render("mypartialview"); // matches /Partials/mypartialview.ctp
}

Which file will be loaded first in cakephp?

I am working with cakephp , i need some clarification regarding initiating.
which file will be loaded first in cakephp whether index.php or bootstrap file
The answer of Ganesh is not really correct. It does not redirect and it does not load AppController as third file, nor does it load it ever directly.
First you should always configure your site to be accessed from the app/webroot folder NOT the index.php in the level above, because if you do that you expose the whole app structure to the public web as well.
When app/webroot/index.php is accessed CakePHP sets a bunch of constants like the CAKE_CORE_INCLUDE_PATH, WWW_ROOT and a few others, the best is to have a look at this file.
Then it will include the bootstrap.php file.
At the end of this file you'll see that not AppController but the Dispatcher is called first and the Request/Response classes are passed to it.
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch(
new CakeRequest(),
new CakeResponse()
);
See Dispatcher::dispatch().
Then, still no controller is loaded. It first fires events and by this dispatcher filters which can interrupt the request and already send data back to the client. That's how the AssetDispatcher works for example. Again, still no controller here.
If the filters passed then the dispatcher will call the controller that matches the requested url, not AppController, if you called /users/index it will instantiate the UsersController and call its index() method. See Dispatcher::_loadController().
All your Controllers should extend AppController but AppController actually never gets called directly.
When a cakephp application is accessed first it will load the index.php from there it redirects to bootstrap.php and then AppController.php.

CakePHP: Passing data to plugin

I have installed a plugin (SignMeUp) for the user registration, and according to the documentation the registration function inside my User Controller has to look something like this:
public function register() {
$this->SignMeUp->register();
}
Looking at the $this->data array inside this function shows that everything is working fine. However, when I use
debug($this->data);
inside the registration() function of the plugin (the one I just directed my function to), the array is empty. Somehow the data doesn't get passed on. What could be the cause?
Resolved! This can be solved by replacing all the $this->data occurences in the SignMeUpComponent with $this->controller->request->data
It's a change from Cake 1.3 to Cake 2.x

Cakephp: Routing and default views

I'm currently trying to add an admin mode to my cakephp site.
I followed a tutorial on the web and added a routing prefix:
Configure::write('Routing.prefixes', array('admin'));
Then I implemented login and logout functionality.
I added a admin_view.ctp and admin_index.ctp to a model where I want to restrict access. Therefore I deleted view.ctp and index.ctp and expected that only admins could view the model by using:
http://xxxx/model/admin/index
But when I entered
http://xxxx/model/index
a default view appeared that I could not disable (it allows model manipulation). Is there a standard way to disable all these default views or do I have to create a index.ctp and view.ctp that simply show error messages?
If you want to disallow certain action, you need to setup ACL, which is described here. And for authentication, in your controller you need something like this:
class SomethingsController extends AppController
{
var $components = array('Auth');
//this will allow all users access to the index
//and view methods ( but not any other)
function beforeFilter() {
$this->Auth->allow('index','view');
}
// other actions
}
Hope this helps.
Delete default controller methods: function index(), view(), add(), edit() and delete(). Removing *.ctp templates is not enough.

Resources