CakePHP - Changing default Flash layout - cakephp

I know that I can replace the flash markup by creating something like custom_flash.ctp in Elements folder and call it like:
$this->Session->setFlash('Hello', custom_flash)
But how can I use custom layout when not adding the second parameter?
$this->Session->setFlash('Hello')
I thought I can replace the default by having a file named default.ctp inside Elements folder. But I can't.
I want to keep the code as short as possible. That's why I'm looking a way to do this
Any solution? Thanks

Try to create your Component:
class MySessionComponent extends Session {
public function setFlash($message) {
return $this->setFlash($message, 'custom_flash');
}
}
and than in your controller just use:
public $components = array('MySession');
$this->MySession->setFlash('Hello');

I found the answer from this question.
We need to add this codes in app/Controller/AppController.php
function beforeRender(){
if ($this->Session->check('Message.flash')) {
$flash = $this->Session->read('Message.flash');
if ($flash['element'] == 'default') {
$flash['element'] = 'fileNameOfYourCustomFlash';
$this->Session->write('Message.flash', $flash);
}
}
}
It basically add element parameter in flash when it doesn't exist yet.

This is explained on the cakephp website here

Related

How to add attributes to ngform object in component class

My form object is JSON.Stringify(form.value) looking like this
"body":{
"panNo":"IRFPP1993A",
"ackNo":"123456789"
}
I want output like this (I want to hardcord one new value inside
"body":{
"requestType":"getStatus",
*panNo":"IRFPP1993A",
"ackNo":"123456789"
}
Please help me ...! Thanks in advance..!
You can add this code in controller
if(form.value){
form.value.requestType = 'getStatus';
}

Organize Models in subdirectories CakePHP 3

we are using subdirectories in our projects no separete views and controllers but in models we didn’t learn yet. Recently I’ve found this https://github.com/cakephp/cakephp/issues/60451 and actually routes and plugins we are already using, we just want to separete our models like this:
Model
-Entity
–Financial
—Money.php
-Table
–Financial
—MoneyTable.php
I’ve tryed put like this then controller is not able to find his model. How can I do to organize it, and make it work?
Things that we've tried:
Use $this->setAlias('TableModel');
Call in controller:
$this->TableModel = $this->loadModel('Subfolder/TableModel');
didn't work for SQL build, and other classes.
CakePHP uses the TableRegister to load models. That class can be configured to use a class that implements the LocatorInterface, and CakePHP uses the TableLocator as the default.
The only thing you can do is configure your own LocatorInterface instance in your bootstrap.php. You would have to create your MyTableLocator and have it change the className for tables to point to subdirectories. What rules for this class name rewritting are used is purely up to you.
bootstrap.php:
TableRegister::setTableLocator(new MyTableLocator());
MyTableLocator.php:
class MyTableLocator extends TableLocator {
protected function _getClassName($alias, array $options = [])
{
if($alias === 'Subfolder/TableModel') {
return TableModel::class;
}
return parent::_getClassName($alias, $options);
}
}
The above isn't working code.
I'm just demonstrating what the function is you need to override, and that you need logic in place to return a different class name.
You can check if the $alias contains the / character, and if so. Return a class name by extracting the subfolder name from the $alias. Take a look at the TableLocator to see how it's using the App::className function.

Grid formatter for uploaded file

I'd like to create a link in my grid to download uploaded files.
There is already several topics on it and the only solution that worked for me is: https://groups.google.com/forum/#!msg/agile-toolkit-devel/degomDwwe1s/gGtcap-T27sJ
But I'd like more of an abstract solution which could work whatever the field name is. So I searched for adding formatter : ATK4 How to set up custom formatters?
But it is never used...
I tried many things (v.4.2.5):
//in my model
$this->add('filestore\Field_File', 'file_id')
->display(array('form'=>'upload','grid'=>myField));
$this->hasOne('filestore\File', 'file_id', 'id')
->display(array('form'=>'upload', 'grid'=>'myField'));
//in filestore\FIELD_FILE
$this->display(array('form'=>'upload', 'grid'=>'myField'));
//my grid
class GridFile extends Grid
{
function format_myField($field)
{
$fm = $this->add('filestore/Model_File');
$fm->load($this->current_row[$field]);
$src = $fm->getPath();
$name = $fm->get('original_filename');
$this->current_row_html[$field] = "<a href='$src'>$name</a>";
}
}
Model:
$this->addField("ipv4")
->display(["grid"=>"foo"]);
Page:
$g=$this->add("CRUD", ["grid_class"=>"XGrid"]);
XGrid:
class XGrid extends Grid {
function format_foo($a,$b,$c=null){
$this->current_row_html[$a] = "<b>".$this->current_row[$a]."</b>";
}
}
Works like charm - perhaps your crud/grid not using the extended version. P.s. using latest atk version (master branch from git)

How to use Ext.create properly

Can't find any relevant information in the sencha documention about this question :
Is it possible to call Ext.create(...) with a parameter which does not depend on the application's name?
So that if I change the app's name I don't have to rewrite that line of code?
Normally I would use Ext.create(AppName.model.MYMODEL) but that's too tied to the app's name for me.
Still need help :)
Create using class alias
When using Ext.define to define your class, you can provide an alias property. You've probably seen this on UI components which use aliases like widget.panel. Those aliases can be used with Ext.create.
Ext.define('MyApp.SomeClass', {
alias: 'app.someclass', // Independent of class name
/* ... */
});
Ext.create('app.someclass', {
/* ... */
});
You can set the alias on a class after it has been created by using Ext.ClassManager.setAlias.
Helper function using application name
If you don't have the option to set an alias, you could create a function that wraps Ext.create which supplies your base namespace automatically.
The problem here is that Ext.application doesn't return the application object. I'm not sure how Sencha Architect generates the application code but you may need additional overrides to allow you to retrieve the application object.
function appCreate(className, config) {
var appName = someMethodThatGetsTheApplicationName();
return Ext.create(appName + '.' + className, config);
};
// Example usage: Creates object of MyApp.model.MyModel
var myObj = appCreate('model.MyModel', { /* ... */ });
How to get the application name at runtime
By default, Ext JS does not retain a reference to the application object when using Ext.application, so we need an override to do it. I'm using Ext.currentApp as the property to store this object, but you can change it to whatever you'd like.
Ext.application = function (config) {
Ext.require('Ext.app.Application');
Ext.onReady(function () {
Ext.currentApp = new Ext.app.Application(config);
});
};
Now that you have this, you can access the application name by simply using Ext.currentApp.name. Or, if you'd feel more comfortable using a getter you can use the following.
Ext.app.Application.addMembers({
getName: function () {
return this.name;
}
});
// Example usage:
function someMethodThatGetsTheApplicationName() {
if (!Ext.currentApp) {
Ext.Error.raise('Current app does not exist.');
}
return Ext.currentApp.getName();
}
You can use any class name in Ext.create there is no naming convention imposed there as long as the class was already defined. If you want Ext.create to load the correct file using Ext.loader you will need to configure the loader to conform with the naming convention you need.
The way to do it :
You need a controller that will in it's INIT function (before UI Loading/Initiating) do the following
APPNAME = this.getApplication().getName();
Where APPNAME is a global variable.
Then when you Ext.create something you will be able to write the following
Ext.create(APPNAME +'model.MyModel');
That way you can change you app name without having to check everywhere in your code to change every single Ext.create to the new app's name.
It also give you the ability if you are to use this.getApplication().setName() to have infinite cache storage has you get 5/10mb per AppName.

Cakephp Localization, Cannot Change language when DEFAULT_LANGUAGE is set

I am confused :)
I'm using the p18n component in cakephp found here:
http://www.palivoda.eu/2008/04/i18n-in-cakephp-12-database-content-translation-part-2/
This component requires me to set in core.php the following constant:
define("DEFAULT_LANGUAGE", 'eng')
However when this is set I cannot change the language using:
Configure::write('Config.language', 'eng');
At the moment, into my knowledge, the only way to change the locale of my static content is the use of the Configure::write. But for the dynamic content to change through the use of the p28n component I must have the DEFINE_LANGUAGE constant set to a value.
This is all very confusing. Any help will be much appreciated.
I'm not familiar with particular component, but I've done this "manually" by setting the same constant in my app/config/bootstrap.php file and then setting the "actual" language to be used in my AppController (copied from the core code to app/app_controller.php). The appropriate snippets of that controller look like this:
uses ( 'L10n' );
class AppController extends Controller {
public function beforeFilter() {
$this->_setLanguage();
/**
* Set the default "domain" for translations. The domain is the
* same as the po file name in a given locale directory. e.g.
* __d ( 'homepage', 'message_id' ) would look for the
* message_id key in homepage.po. Using the __() convenience
* function will always look in default.po.
*/
$this->set ( 'domain', 'default' );
}
private function _setLanguage() {
$this->L10n = new L10n();
# Auto-detect the request language settings
$this->L10n->get();
}
}
Pretty vanilla stuff, but it works great. And breaking out the _setLanguage() method allows for the use of different methodologies to determine locale (e.g subdomain like fr.mydomain.com).

Resources