What is the logic behind cakephp set function, that the variable is available in view file after set from controller (How this happens?)
Here is the code for the set function:
CakePHP's set function
To fully understand it you should also read the render function. In CakePHP views are assumed to have the same name as the public function so it's not common to see render in the controller as you would see a lot, for example, in Yii Framework.
CakePHP's render function
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
Follow up question to: In CakePHP, where would I put a method that performs a check on the Session User?
Background: In my previous question, I was informed that the correct place to place a method such as levelCheck() which performs a semi-complicated evaluation of a user's level, and is needed in practically all my controllers, is the AppController.
Now, this method is also incredibly useful for the way I design menu layouts and other view devices. For that reason, I would like to create a helper that can make use of that method.
Problem: I recognize that it's generally frowned upon to call a controller method from the view... however there is no viable way for me to pass data to replicate the function of this method, nor do I want to replicate the method in two places (violating DRY methodology).
Question: How then do I call this method from a helper, or is there a better way to provide use of this method in the view/elements?
Put the method in AppController, also - set a variable that will save the result.
In the beforeRender callback, set the result as a viewVar.
some code:
// AppController
class AppController extends Controller
{
$levelCheckResult = null;
function levelCheck(){
$this->levelCheckResult = true/false;
}
function beforeRender(){
$this->set('levelCheckResult', $this->levelCheckResult);
}
}
that's it, now you can access it in the view.
edit
create a public function in the app_controller, and in the beforeRender(), send the app_controller itself, to the view.
class AppController extends Controller
{
function levelCheck(){
....
}
function beforeRender(){
$this->set('TheApp', $this);
}
}
// in the view
$TheApp::levelCheck();
However, please consider again the design. this kind of manipulation is strongly suggest that you should change some stuff there.
In short - the view is still backend execution of the program and not the client side, so in definition - it should and can be controlled, from the controller...
The solution I ended up using was to move the method to a component (CurrentUserComponent).
From there, it was as simple as calling the component in the head of my helper...
App::uses('CurrentUserComponent', 'Controller/Component');
And referencing the component's static method:
CurrentUserComponent::levelCheck(x,y,z);
I don't think this is entirely within the intention of the MVC pattern, but it doesn't pervert it horribly, and allows access to the method from anywhere in the application.
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);
Morning guys,
So this is my first time developing a plugin for CakePHP. Here's what I am doing in startUp of the component.
//component
function startUp(&$controller){
//....
if($render){
$controller->render("return", "ajax");
}
}
By default render will look at app/views/<controllers>/return.ctp and app/views/layouts/ajax for this render call.
Is there anyway that I can give a directive to render from app/my_plugin/views/awesome_stuffs/return.ctp and app/my_plugin/views/layout/ajax.ctp instead?
I believe the third param of Controller::render($file, $layout, $file) could do the job, but is there any better Cake way of doing things?
Plus, is that considered a good practice to take over controller's rendering function like that?
One way is to call the PLUGIN controller/action URL in your AJAX call, instead of the main app controller/action URL.
ex:
instead of:
http://domain.com/controller/action
you call:
http://domain.com/my_plugin/controller/action
When you do it this way, the plugin views & layouts are called automagically. See:
http://book.cakephp.org/view/1118/Plugin-Tips
http://book.cakephp.org/view/1115/Plugin-Views
Otherwise, the only way I know of is manually setting paths as you mentioned or controller-wide via:
var $viewPath = 'path/to/plugin/views/';
var $layoutPath = 'path/to/plugin/layouts/';
You might want to try setting $this->view to the plugin dotted view file you want to render.
add to your source
$controller->plugin = "pluginname";