How to access current request from bootstrap? - request

Is it possible to access the current request in Kohana's bootstrap? I tried accessing Request::$current but $current doesn't seem to be defined at that stage. Is there any way around that? Also at what point in the application is Request::$current defined?

It's not possible, because Request object is created in index.php after including bootstrap.php:
// Bootstrap the application
require APPPATH.'bootstrap'.EXT;
/**
* Execute the main request. A source of the URI can be passed, eg: $_SERVER['PATH_INFO'].
* If no source is specified, the URI will be automatically detected.
*/
$request = Request::factory();
If you must access it, do it in the index.php after it has been created, although maybe you could tell us what exactly are you trying to do?

You can use it after Kohana initialization.
Kohana::init(...);
Also, good practice is using interface methods instead of public variable. I'm wondering why the developers keep $current as a public field.
So.. use
Request::current();
Also, It seems that using
Request::initial();
is better idea. But it depends on your realization.

Related

Apache access the server_rec for a module

I want to get at the value in server_rec.module_config. Does request_rec have any sort of reference to the server_rec? I am not seeing one in the httpd.h file but I could be missing it.
Basically I want to access the configuration file for the module while in my handler and I know I can do that with ap_get_module_config(). However I don't think request_rec.request_config or request_rec.per_dir_config store what I need.
Help is appreciated! And yes I have looked through the Apache guide to making modules (found here).
After more looking around I finally managed to come across it.
In the request_rec there is a variable called server which is a server_rec*. So to access the config file you would use
ap_get_module_config(r->server->module_config, &my_module)
where r is the request_rec passed into the handler.

Loading CakePHP Helpers

This is a multi part question.
Background:
I'm building my first site using CakePHP, and I like it so far. I've got a DB setup, initial data loaded, and a few models, views, and controllers to interface with the data.
I've created a globally accessible function to create Add/Edit/Delete type image links in various areas. It will be used across multiple views, so I need it accessible, essentially, everywhere. The function is defined in /app/config/bootstrap.php. I was hoping to use the HTML Helper's $html->image() and $html->link() methods to facilitate this, but they're not available in bootstrap.php and I'm not sure how to load/access the HTML Helper where I've defined my function.
Questions:
1) Is this a reasonable/idiomatic place to define a function of this sort?
2) If this isn't the correct place to define the function, where should I define it?
3) If this is the correct place to define the function, how can I go about loading various CakePHP helpers?
Again, I am new to CakePHP, so please let me know if my question is unclear, and forgive my ignorance. I've read/searched through a fair amount of the CakePHP documentation and while I can find plenty of references to loading helpers within Controllers via App::import(...); or $helpers = array(...);, I do not seem to have access to the App object and the $helpers member is specific to the AppController class, I assume. I assume I'm going about this incorrectly, so please help me understand the Cake way of accomplishing this.
No, that is not the correct place for such a function (or more accurately, it goes against the MVC paradigm). A better approach would be to create your own helper for the function. Depending on the complexity of the links you could also use elements.
As a rule of thumb only functions that are completely independent of anything else in the app should be in bootstrap.php, and even most of those would often be better somewhere else.

Getting controller's name inside behavior

Im writing ClearCache behavior.
It's purpose is to delete some of custom cache files on every afterSave and afterDelete event of the model.
In order to delete right files i need to know name of controller and the name of action that called ModelWithClearCacheBehavior->save() or ModelWithClearCacheBehavior->delete()
My question is:
How to get those names inside behavior?
There is no an elegant solution about this (at least I don't know it).
You can do it with a Configure::write class for example:
in your AppController's beforeFilter() you can add the following code:
Configure::write('current_controller', $this->name);
Configure::write('current_action', $this->action);
later on in your behavior you can access them with
Configure::read('current_controller');
Configure::read('current_action');
You can access it because you set them before any model iterations.
For sure it's not elegant but it's working.
Not something I've really done anything with, but a brief reading of the book seems to indicate that the model is (or should be) available inside the behaviour -
When creating behavior methods you automatically get passed a reference of the calling model as the first parameter. All other supplied parameters are shifted one place to the right.
You should then be able to access the model via $Model
this is a bit late but for future reference, in cakephp 2.0 can be done this way in a behavior (using CakeRequest)
beforeFind(&$model, $query){
global $Dispatcher;
$request = new CakeRequest();
$request = $Dispatcher->parseParams($request, $additionalParams = array());
pr($request->params->controller);
return $query;
}

Problem in sharing Cache between main domain and subdomain

As I have posted before also,I am developing a site in two languages done in CakePHP.
Now the latest problem I am facing in my application is that the application is not sharing the Cache values between the Main Domain and the Subdomain.Please help me out of this mess...!!!
Thanks in advance.
There are 3 problems connected to this.
Data you're caching manually by using Cache::read() and Cache::write()
Just use a prefix for each subdomain when using the methods.
Element caching
you can solve this "almost" elegantly by following these steps:
Create my_view.php (or whatever) in app/views
Content:
class MyView extends View {
}
Search for view.php in the Cake Core and copy the element() method to your newly created class. Add your subdomain prefix in the part where the caching happens
In your AppController::beforeFilter() write
$this->view = 'MyView';
Now you have control over the CakePHP view layer. You have just overridden the element method.
Alternatively to this approach (if your codebase isn't already using elements extensively) you could just create a helper with a method, that takes the same arguments as the View::element() method, add the subdomain key to the cache options and call the orginal element() method.
Full Page Caching
This is a tricky one. The full page caching happens in the dispatch() method before you have any possibility to modify the behavior. The second problem is, that CakePHP uses the relative URL of the page to cache it. The relative URLs are most likely identical under your different subdomains.
I think the simplest approach here is to create a Dispatcher class, which extends the original dispatcher. Override the cached() method and implement your desired behavior, like the prefixes. Then in your app/webroot/index.php you need to change this line
$Dispatcher = new Dispatcher();
...to your new class name.

cakephp requestHandler check for swf/flash

Is there a way to check if a cakePHP action is being called from an swf/flash movie like there is for Ajax using the requestHandler?
Put a named parameter in the URL that Flash is requesting:
eg. http://www.example.com/controller/action/flash:true
Check for this named parameter in your controller (or AppController) code:
if (isset($this->params['named']['flash'])) {
...
}
I don't believe so. A better option might be to create discrete controller actions that you only use from your Flash app.
Not the way I think you mean. The requestHandler can detect the type of request, but I think you're looking for the request source. It might be worth trying the getReferrer() method, but you may end up needing to add a click handler to the swf (if it's yours and you have that access).

Resources