CakePHP custom function (but not global) - cakephp

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.

Related

Replacement for AppModel in Cakephp3

I would like to implement an autoslug-function for two models in CakePHP3 and use the beforeSave-callback for that which works fine. However the slug structure is a bit different than in Inflector::slug, so I wrote a small function for the different structure which finally leads to my question
In Cake2 I would have placed this helper function in AppModel which is not existing anymore. What's the best way to do that now? A behaviour (which seems a bit biggish for 2 lines of code) or class AppTable extends Table or ..?
Make it a behavior and use it where needed instead of putting it in a super model class. If you put that into a plugin and repository you can simply add your plugin as package via composer for every app that needs this plugin.

cakephp - how to define cake php helpes correctly

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.

CakePHP default layout for xlsx

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 :)

How to create a simple 'Hello World' with two language in cakephp?

i have been searching for easy sample of multi language project in cakephp.
but i can't find this sample. i think should use i18n but i can't use.
can help me for this sample?
Basically you've to combine the link that Matt gave you, which is the way to use translation on static texts on your website for ex. like links "More..." etc, with Translate behavior, which as mentioned before is based of i18n and will give you the possibility to use Translation on your models stored in DB.
Please check this page in book: http://book.cakephp.org/1.3/en/The-Manual/Core-Behaviors/Translate.html hope this will be helpfull
you need to create a controller in app>controllers> (create the controller here)
Then you need to declare an action in the controller.
For example in the examples_controllers.php controller file:
ExamplesController extends AppController{
function anyName() {
var $uses = null;
$this->set('hello', "hello world");
}
}
to call this from view:
create a folder under views (make sure give the name same as the controller name)
For above controller, the folder name will be examples.
Then in the folder, create "any_Name.ctp" file. (the name should be as the action name)
In that ctp file just call that variable hello. That will do.
I surfed through the web and got a complete solution in here, on my site:
http://www.getallthing.com/cake-php-hello-world/
Best of luck! Cheers!

How do I code a MVC3 Helper

I’ve just build my first Helper in MVC, it’s very basic and just displays a string where ever I use it. So it’s a .cshtml file in my App_Code folder, I think that is how it's supposed to be set up, with the following code in it,
#helper DisplaySelect() {
#:This text is coming from an helper class.
}
Now I am a wiz with helpers how do I make it do things. E.g.. say I want it to query the database and display something, I would normally do that work in my controller. How do I do that with helpers, do I create a helper controller and then treat the helper like a partial view???
Any help would be greatly appreciated.
Cheers,
Mike.
Thanks guys,
I’ve asked that question before Shark “Why would you use a helper and not a partial view” the answer I got there was a partial view is more for just displaying common HTML where as a Helper can have a bunch of code in it and do all kinds of great processing stuff. Now it seems that’s not true and they are pretty much the same thing, in some respect, except in Link664’s case!
I like what you’re saying Link664 that makes sense as it cleans up the code nicely.
What I was going to do was try and populate a drop down list in a helper and then use it in multiple places, but from my research today that’s not what helpers are for.
Cheers,
Mike.
I'm a bit confused by what you want to do and why you want to do it but I'll give it a go. As you mentioned, you would normally do that work in your controller. It is very poor design to make database calls from a partial view/html helper created client-side.
The #helper syntax is to be used only for simplifying view code, not for implementing code that should be in the controllers or models in the view. See this article for a better idea of when you should be using them.
As for your example, I recommend you pass the data that you want from your controller and create an extension method on HtmlHelper to render it in your view. For example if you wanted to display a list of the most recent 10 posts on your view:
public static HtmlString RecentPostsDisplay(this HtmlHelper html, string name,
List<Post> values, object htmlAttributes)
{
var tag = new TagBuilder("ul");
...
//build list content by looping through values and adding to TagBuilder
...
return new HtmlString(tag.ToString(TagRenderMode.SelfClosing));
}
and in your controller
public ActionResult Blah()
{
ViewBag.posts = _db.GetMostRecentPosts();
return View();
}
Then in your view you can put
#Html.RecentPostsDisplay(ViewBag.RecentPosts)
Hopefully that is what you are looking for, if not update your question so it's not so ambiguous and I'll try again!

Resources