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
Related
I am confused about the distinction and treatment of the index() and view() functions inside CakePHP 2.4.
Specifically, I am trying to modify a 3rd party API authored in CakePHP 2.4
Consider this Controller:
class EventsController extends AppController {
<snip>
public function index() {
if ($this->request->params['named']) {
$conditions = $this->request->params['named'];
} else {
$conditions = array();
}
}
This allows me to construct a URL like http://myserver/api/events/index/StartTime >=:12/EndTime <=15.json and the StartTime and EndTime get passed into conditions when I do a find.
That's great.
Now that same Controller has this additional function:
public function view($id) {
}
This function seems to be called when I invoke a url like http://myserver/api/events/1234.json
I am trying to extend view with more parameters, just like index. For example, I'd like to invoke:
http://myserver/api/events/1234/MonitorId =:3.json and MonitorId =:3 gets passed as a parameter to handle in view. I don't want to add these into the function definitions - it can be anything.
If I try and do this, I get a "Controller" not found, but this same approach works in index()
Can someone help me achieve my goal and help explain what is going on?
(IF it helps, the full code of the controller is here)
As I can see, you are using CakePHP RESTful routing routes.php
Here,
index is used for listing all the resources e.g. all blog posts. URL /events/index is getting mapped to index method in controller automagically.
view is used for showing a specific resource which can be identified by some unique identifier like id or uuid e.g. specific blog post. URL /events/<some_identifier> gets mapped to view method
What you need to do is modify view definition to read all query string parameters that you pass in URL and call Model accordingly. By default. routes parses the first string after events in URL to resource ID and passes it as first argument to view method in controller. This is scaffolded code.
You can use named parameters $this->request->params['named'] in your view definition and construct your queries. No hard-coding.
It turns out that I need to explicitly call the view action to pass it more parameters.
So api/events/view/218245/AlarmFrames >=: 80.json works as intended and then I can get the parameters with $conditions = $this->request->params['named'];
I am using cakephp 3.x have put database queries in model in which i want to check the current controller action and based on that i will make my database queries.
I know i can pass controller action from my controller itself to my model function but is there a way i can just check my current controller action inside my model only so that i do not need to pass it in my multiple functions which are inside model only.
My Try -- UsersTable.php
public function initialize(array $config)
{
parent::initialize($config);
$this->table('users');
$this->primaryKey('user_id');
echo '<pre>';
print_r($GLOBALS);
exit;
}
so far if i do this, i got an array in response and in which i found this the best result out of other things in that array
[REQUEST_URI] => /5p_group/users/add
I m also trying to use this
echo '<pre>';
print_r($GLOBALS['_SERVER']['HTTP_REFERER']);
exit;
which gives me this output
http://localhost/5p_group/users/archived
so eventually i am getting the result which i want but i want another proper method which cakephp 3.x uses ..
So is there any other way or more frequent way from that i can get my current controller action ?
Any ideas will be appreciated ..
Thanks
First of all you should use the routing class in your model. So at the top you can call it like below
use Cake\Routing\Router;
And then in your initialize function you can check the params like
debug(Router::getRequest());
Or to be more specific you can use
debug(Router::getRequest()->params['action']);
I think this is the solution using cakephp classes only, though a small hack.
For more function reference you can access the cookbook at Router Class
The question was about a Model, but it shows up on Google.
When inside a view (.ctp file), use
\Cake\Routing\Router::getRequest()->getAttributes();
CakePHP 3
why there is mandatory index function in cake php and how we can make an other function in a class, because i made a class in a controller and and also made a function which is index function but when i want to changes that function name there is an error? Kindly tell me if any one knows about it?Why we need to make index function and how to define that and how to make a new function in same class? I have also search but could find any satisfactory answer.
Here are some links
http://book.cakephp.org/
CakePHP check if user is logged in inside a view
Its not mandatory to have an index() function in the controller and cake will never ever generate error for this. Index function is only made, so that anyone hitting your controller without any action never sees the error related to framework.
Its a good practice to have index() function in classed and index.html or index.php file to avoid directory access.
It is not mandatory to have index function in the controller, but if no action exist then by default cake search for index function, because by default index is searched by browser.
I followed Andrew Perkins excellent tutorial on setting up permissions in CakePHP 2.0.
My question, however, relates to how to use the allow and deny method in the Pages controller. Currently I have $this->Auth->allow('display') which allows all methods in the Pages controller to be view.
What if I only want the home page allowed but the rest denied? How do I code that?
Thanks in advance.
Make sure you have copied the PageController.php to your app/Controller folder. Then, add a beforeFilter callback method and set access based on the passed page parameter:
public function beforeFilter() {
// Use $this->request->pass to get the requested page name/id
// Decide on access with $this->Auth->allow()
}
This should solve your problem.
You can find more information on request's lifecycle in CakePHP manual. That's pretty useful stuff.
Have you tried this code?
You can out it into your PageController or into your Controller directly
$views = array ('index'); //array of view that you want allow
$this->Auth->allow($views);
i need to prevent a view to be rendered in a specified case but i can't understand how to prevent it to render.
I tried
$this->autoRender=false
but nothing happened, probably because i'm using an API engine that manage rendering differently from regular controllers. Anyone know any trick to do this?
Using $this->layout = 'ajax' does not seem to be enough.
But using these both lines works:
$this->layout = 'ajax';
$this->render(false);
While searching for a solution, I found this answer. Now when using CakePHP 2.4.x, you could use the following code in your controller:
$this->layout = false;
This will lead to just the view being rendered, without a layout.
It's an old question. The current cake-version is 3.x and there is a easy way to use a blank layout.
Only add the in the controller:
$this->viewBuilder()->autoLayout(false);
Try to use ajax layout $this->layout = 'ajax' this is the default empty layout, which is used for ajax methods.
public function function_without_layout(){
$this->viewBuilder()->autoLayout(false);
echo "hello Brij";
exit;
}
$this->layout = false; is deprecated in CakePHP version 3.
Use $this->viewBuilder()->autoLayout(false); for CakePHP version 3.
Add this in your controller:
$this->autoRender = false;
This works in my project.
The CakePHP 3 autoLayout(false) method from the other answer will still have the system try to locate a corresponding view/template file for the action you're calling. Since I needed no output at all, this didn't work for me, so I needed to also render an empty template.
Creating a blank .ctp file for every empty action you might need isn't an option really, because you'd normally want to have one and reuse it. CakePHP 2 had a $this->viewPath property which would let you configure the controller to look into the app/View folder, but it's CakePHP 3 alternative still looks into the corresponding controller and prefix folders. There is a not-so-obvious way to force CakePHP3 to look for a template in a root view path.
Create src/Template/my_blank_view.ctp
Add the following to your controller action:
$this->viewBuilder()->layout(false);
$this->viewBuilder()->templatePath('.'); // this
$this->viewBuilder()->template('my_blank_view');
Also, I'm using $this->viewBuilder()->layout(false) instead of autoLayout(false) because the latter kind of implies that there might be another layout set later, where the layout(false) just explicitly sets that there's no layout needed.
without knowing anything about API engine you're using, maybe try to make empty layout with empty content and call it in controller as $this->layout = 'empty_layout'