CodeIgniter Forums
Where to put message file in codeigniter4? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Where to put message file in codeigniter4? (/showthread.php?tid=87776)



Where to put message file in codeigniter4? - mayurkoshti - 05-31-2023

I want to create a separate file called custom_message.php. What will be the location of this file in CI4 project structure and how to call in controller.

Example:
Code:
$arr_custom_message = [

'msg_add' => 'Admin Created!',
'msg_update' => 'Updated Successfully!'

];



RE: Where to put message file in codeigniter4? - InsiteFX - 05-31-2023

You can create your own folder under the app folder

app/Messages

And place your files there.


RE: Where to put message file in codeigniter4? - mayurkoshti - 06-02-2023

(05-31-2023, 11:34 PM)InsiteFX Wrote: You can create your own folder under the app folder

app/Messages

And place your files there.

Thanks!


RE: Where to put message file in codeigniter4? - InsiteFX - 06-03-2023

Don't forget you can also have your own Modules.


RE: Where to put message file in codeigniter4? - Muzikant - 06-05-2023

Hi. For simple messages like this you can use config file dedicated to messages.


Create: App/Config/CustomMessages.php
PHP Code:
<?php

namespace Config;

use 
CodeIgniter\Config\BaseConfig;

class 
CustomMessages extends BaseConfig
{
    public $add 'Admin Created!';
    public $update 'Updated Successfully!';


Usage in your controller:
PHP Code:
// ...

$msg config('CustomMessages');
echo 
$msg->add;
echo 
'<br>';
echo 
$msg->update;

// ... 



RE: Where to put message file in codeigniter4? - ikesela - 06-05-2023

suggesting using language file is easier.

create file in folder folder:
App\Language\en\message.php

File content:

return [
'add' => 'Admin Created!',
'update' => Updated Successfully!'
];

calling:
<?=lang('message.add')?>

<?=lang('message.update')?>

* language/localization is flexible, can pass value to message

This from guide:
Code:
<?php

// The language file, Tests.php:
return [
    'apples'      => 'I have {0, number} apples.',
    'men'         => 'The top {1, number} men out-performed the remaining {0, number}',
    'namedApples' => 'I have {number_apples, number, integer} apples.',
];

// Displays "I have 3 apples."
echo lang('Tests.apples', [3]);