I have a method which exports riport data into an xlsx file in my CakePHP 2 project. It is working well. In the same time it seems it just ignores anything what I put into View/Layouts/xlsx/default.ctp
In my routes.php I have
Router::parseExtensions('json', 'xlsx');
In my controller I have
public $components = array('RequestHandler');
My View/Riports/xlsx/export.ctp is rendered, but View/Layouts/xlsx/default.ctp is ignored.
What do I miss?
I think you're out of luck here. From the documentation;
The data view classes don’t support layouts. They assume that the view file will output the serialized content.
http://book.cakephp.org/2.0/en/views/json-and-xml-views.html#using-a-data-view-with-view-files
But I may be wrong of course :)
Related
I'm trying to use themes with CakePHP 2.5.X and I'm failing to load the theme and associated layout inside that theme. What I've done is create "app/View/Themed/Hamilton", and within that directory I've created, "View/Layouts/default.ctp". In "app/Controller/AppController.php" I've done this:
public $viewClass = 'Theme'; /* I've tried $view, it does not work. */
public $theme = 'Hamilton';
public $layout = 'default';
Sadly it loads "app/View/Layouts/default.ctp", not "app/View/Themed/Hamilton/View/Layouts/default.ctp"
I've search stack overflow, no article that talks about this has an answer that works. All debug outputs the proper layout, theme, etc. I've checked spelling and case. It just simply is not loading. Anyone have a fresh perspective?
UPDATE: And once I ask the question I find the solution. The path was wrong to the layout. It should be "App/View/Themed/Hamilton/Layouts/default.ctp" there does not need to be another "View" folder.
Hopefully this may help someone else who has the same issue.
According to the docs, the Layout should be in /app/View/Themed/Example/Layouts/, so in your case it is /app/View/Themed/Hamilton/Layouts/default.ctp, not app/View/Themed/Hamilton/View/Layouts/default.ctp as you said.
I have an ItemsController which extends AppController.
To handle my site menu, I used a menuhelper.
However, I saw in the documentation that you have to add your helpers in an array in your controller (AppController):
public $helpers = array('Form', 'Html', 'Menu');
The weird thing is that I forgot it and my menu worked anyway.
Also, you always have to add Form and Html extra to the array because otherwise they don't work anymore.
However, when I do something like this in my AppController, my form helpers still work:
public $helpers = array('Menu');
So it seems that whatever I do, it still works, but I don't get why and I don't like automatic "magic" :)
Is there something I am missing in the docs?
Since 2.x you don't "need" to specify app or core helpers.
Those are lazyloaded automatically.
You only need to specify plugin helpers manually.
That said I personally still always describe what helpers I use, just to be consistent with the plugin ones.
I have a module that handles translations.
It is not bound to any database, the file is purely and simply something like this:
$arr["key1"]="text";
...
I need to make Ajax calls in order to edit that file. That file will be imported each time a page is accessed in order to deliver any text content that I need (it may not be the best thing of the world but it does the job, and it's supposed to be really fast since there's no "XML parsing" kind of thing, it is simply stored in a standard .php file).
To handle these ajax calls that will have actions like Add/Edit operations, I have made a TranslationController that is Model-less:
class TranslationsController extends AppController {
public $uses = array(); // Model-less
public $components = array('RequestHandler');
public $helpers = array('Session');
protected $translationFilePath;
public function setItem() {
}
public function backupFile() {
}
}
My problem is simple: where do I put custom functions that are used to open/write/find in files?
For example I have a function that extracts a key from the line that is being read.
I've seen some posts where we are advised to place the data inside the model but in my case I have no model AND it's these functions are not linked to the data. So it shouldn't be in the model, right? I do not want it to be global.
Thank you for your help.
Why are you not using the built in translation stuff that comes with CakePHP and is based on the more or less standard gettext tools?
What you're doing is just re-inventing the wheel and probably coming up with a non tested customized translation implementation that is obviously lacking feature that CakePHP alreay offers you. For example how do you handle plurals?
CakePHP offers you __(), __d(), __n(), __dn() for translations and the translation files are stored in APP/Locale/ as plain text files and are edited with poedit.
See http://www.gnu.org/software/gettext/ and http://book.cakephp.org/2.0/en/core-libraries/internationalization-and-localization.html
Sounds like a use case for a custom datasource: http://book.cakephp.org/2.0/en/models/datasources.html.
Personally, I would just create a normal PHP class in the app/Lib folder of your application.
I am trying to display an uploaded image with the Media Plugin of CakePHP.
I added the helper to the controller helper array: var $helpers = array('Media.Media');. Then, in my view, I have this code: echo $media−>file($news['Attachment'][0]['dirname'].DS.$news['Attachment'][0]['basename']);. But the problem is that, it outputs this error:
Undefined variable: media− [APP/views/news/view.ctp, line 3]
What could be the problem?
By the way, if a plugin has a model User in app/plugin/users/models/user.php and i create a new model called User in the app/models folder which one will be loaded?
Thanks in advance for any help!
First off if you are using 1.3.x refer to helpers via $this->HelperName->method(), there could be a variable called $media being set in some method. you can check this by doing var_dump($media);
The other option is that something has maybe unset it. Its very strange that you have the helper set but the variable is not set. It could also be due to adding the $helpers array to the wrong controller, you can try add it to app_controller and see if that works. if it does you had it in the wrong place.
If i got your second question correct, and we are talking about auto loading, a plugin controller will first look for the model in its own plugin directory, if it is not found there it will fall back to the app/models directory.
if you are loading it manually via the $uses array, it depends on the version of cake and how you do it. In previous versions 1.x even $uses = array('User'); would load the plugin model as cake would auto add the plugin prefix. This has changed for 2.0 afaik.
For other methods of loading a model, such as $this->loadModel('User); would load from app/models and $this->loadModel('PluginName.User') would load from the app/plugins/plugin_name/models dir.
Edit:
you are right that is funny having the error show $media- and there is the problem. did you copy that code from some site? − is not - you have a utf8 char in the code which is what its complaining about.
I just finished the "15 min Blog Post tutorial" included in the documentation for cakephp. I was asked for another tutorial to change the layout for first tutorial.
However, I am fairly new to MVC programming/Cakephp and I have no real clue how to do so. Well, I know I need "default.ctp" placed in app/views/layouts/ and I presume I need to include
to include my data? . . .
I am really at a loss of what to do. I set up my default.ctp as I mentioned above, but when I go to localhost:9999/posts the layout is still the same. I guess I need to include a stylesheet (and if so, where?)
I guess if someone can point me in the right direction to a beginner's guide to layout styling or how to use it I would greatly appreciate any help.
I would advice you to read the following from the cookbook: Layouts and CSS. Then copy the layout from /cake/libs/view/layouts/ to /app/views/layouts/ and modify it to your needs. After that create you stylesheet (or modify existing one) in /app/webroot/css/ and include it in your layout.
Create in app/View/Layout a file named "my_posts_layout.ctp"
In your PostController set $this->layout = 'my_posts_layout';
This way you should view the content defined on my_posts_layout.ctp.
Lack of stylesheets has no impact here.
How MVC works in CakePHP:
The router dispatches an incoming request to an appropriate Contoller.
The appropriate Controller function executes (no output, just fetching data, setting up variables).
The appropriate view is rendered. In fact, the output of the view is just contained in $content_for_layout.
What you really get back in the browser is in the layout. Therefore you can put your view's output into the layout by echo $content_for_layout in default.ctp. (Of course you can also have different layouts.) In addition, the layout can be enhanced with elements.
I really recomend the CakePHP CookBook, easily found from the CakePHP homepage. Modifying default.ctp should edit your applications layout.
A more specific question (eg. code samples of your default.ctp, expected results etc) might help people provide a better answer than mine.