how I use flash message in yii2? - php

how I add flash message to my site using related controller please explain step by step i'm new to the php.
I use yii2 framework to build the site and i need to print flash message in the index page using create controller.

Currently the question is too broad, but here is basic usage:
1) You can set in controller like that:
\Yii::$app->session->setFlash('flashMessage', 'Hello world!');
2) Then you can display it in view like so:
echo \Yii::$app->session->getFlash('flashMessage');
Optionally you can check the existence with:
\Yii::$app->session->hasFlash('flashMessage');
Official docs:
Session
setFlash()
hasFlash()
getFlash()
There are actually more methods for working with flashes, you can see it in official docs.
Also advanced template provides useful widget Alert that integrated with Boostrap 3:
\Yii::$app->session->setFlash('error', 'This is the error message');
...
echo Alert::widget();

For better understanding of working with flash messages, go through # http://www.yiiframework.com. By following this method, you can done printing flash messages in your web page.

Related

In OctoberCMS how do you find the hint path?

Mail::sendTo($to, 'OctoberCMS.PhotoElegance::mail.contactform', $params);
When this line of code is called I get back an error which says the following:
"No hint path defined for [OctoberCMS.PhotoeElgance] on line 112 of vendor\laravel\framework\src\Illuminate\View\FileViewFinder.php"
In simple, I am trying to send mail when the contact form is submitted. The code is inside a function called onStart(). I discovered that the appname is OctoberCMS. After the dot I have PhotoElegance as that is the website name.
After the hint path (OctoberCMS.PhotoElegance) I am calling the static function (mail.contactform) to my defined contact form which has been built in the CMS.
Where do I find out the appname and website name. How do I define these if I want to change them. I understand this is to do with name spacing but I cannot figure it out.
hi the way you try to send an email is called "Mail view" this is the way you do send mail form a plugin, and the path to the mail view is author.plugin::path.to.view.
From the very short definition of your problem i can tell that you are not using plugin but the dynamic pages feature of october cms so you must use "mail template" .
Go to setting > find mail->mail template in the left side bar > click new template chose a code once done you can use this tempalte to send email from dynamic page.
Mail::sendTo($to, 'template.code', $params);

How to configure Vimeo-Service-module in Silverstripe

Forgive me for my English
I am new in PHP. And I'm building a site using Silverstripe and trying to configure Vimeo-Service-module. I'd follow the steps from this link
https://github.com/r0nn1ef/Silverstripe-Vimeo-Service-module
I did everything that mentions in the article. And created a page in admin panel of VimeoGallery page type and set the parameters on Videos tab to grab the videos for display.
After created page, I visited my and clicked on video menu but then all I see is no videos returned. It is showing blank page and no any error messages.
Is that I've done anything wrong. Please guide me...
Thanks in advance.
OK, I think I see the issue here. You are calling VimeoService::setAPIKey() however accessing the method like that is deprecated in the new version (the 2.0 branch - I was incorrect in my comment when I mentioned master) of the module.
The module instead uses the Site Config in the CMS to set the API key and a few other settings.
Now just remove VimeoService::setAPIKey() from your _config.php file, run /dev/build and set the API key through the CMS.
EDIT
On line 142 of VimeoGalleryPage.php, there is a function called flushCache. Replace the code in that function with the following:
public function flushCache($persistent = true) {
parent::flushCache($persistent);
unset($this->_cachedVideos);
}
Basically, the code in the 2.0 branch for this function does not correctly extend the same named function in SiteTree.

PHP routing: display flash messages in View (Klein)

I am using Klein php routing for a simple app
the documentation is ok for using the library, however it is not good at how to implement the views
for instance I want to display a flash message on success/error/warning etc
i understand how klein can store the flash like this error message in documentation
$klein->respond(function ($request, $response, $service, $app) use ($klein) {
// Handle exceptions => flash the message and redirect to the referrer
$klein->onError(function ($klein, $err_msg) {
$klein->service()->flash($err_msg);
$klein->service()->back();
});
so for my success message i did this
$service->flash("Success", $type = 'success' );
but other than foreach on the $_SESSION __flash, i cannot see how to implement this in my view
i surely think there is a render() or something i am missing...i mean otherwise why call all this when i can just store my own msg in a session, doesnt make much sense to me
anyways, any help is always appreciated
If you look at the docs for the latest version, there is a method Klein\ServiceProvider::flashes():
http://chriso.github.io/klein.php/docs/classes/Klein.ServiceProvider.html#method_flashes
The docs say it retrieves and clears all the flash messages, or all the flash messages of a given type.
This is not to be confused with Klein\ServiceProvider::flash(), which adds a flash message:
http://chriso.github.io/klein.php/docs/classes/Klein.ServiceProvider.html#method_flashes
Here's the source:
https://github.com/chriso/klein.php/blob/master/src/Klein/ServiceProvider.php#L179
It looks like it returns an array of flashes, grouped by type, so you could foreach and echo them. If you're using the Klein templating system (in your case, you would render the template with $klein->service->render($myTemplateName)), then you can call the ServiceProvider from the template as $this.
So in your template, you would have something like:
<? foreach($this->flashes() as $type=>$messages): ?>
<? foreach($messages as $msg): ?>
<div class="alert alert-<?= $type ?>"><?= $msg ?></div>
<? endforeach; ?>
<? endforeach; ?>
Obviously, you don't have to use the alternate control structure syntax, but I like to use it in my templates. It was part of the coding standard at a dev shop where I worked, and I adopted it as my own.
Just a heads up. The docs seem to represent the code in dev-master, rather than the 2.0.x branch they tell you to use on the GitHub page. A lot of code seems to have been moved around since then (at least we know it's not abandoned, right?). I found the dev-master branch to be much less broken.

Kohana success/error messages method

I am slightly confused as to what the best method is of handling redirecting and displaying error/ success messages using an MVC framework, specifically Kohana.
I have a Controller User which extends the Base controller.
Am am trying to use the action_remove() function in the base controller then redirect back to the page they were on and display a message 'User has been removed....'
I don't want to pass the error message in the GET params. Is there a standard way of doing this?
You should try to use flash session data. It is very useful when You want to show errors as well as messages. At first access flash data is removed so it can be accessed only once.
http://docs.kohanaphp.com/libraries/session#flash_session_data
Also there was some related post about this here Which is the best way to display 'flash messages' in kohana v3?
You can use Message Modules in kohana 3.x. its used to display messages.
please download this module it from here and extract . Then paste it in modules folder.
https://github.com/GoldCoastMedia/kohana-flash
Then enable it in applications/bootstrap.php like as follows.
'message' => MODPATH.'message',
There are 5 type of messages are available. success, error, warning, info, notice. You can give styles for each messages . but you need to write class in the same name of message type.
Message::error('pls login to access');
//to assign message type.its error message.
echo Message::display();
//to display it
thats it. but remember that you need to create class in the name of success, error, warning, info, notice to apply styles.
to check condition in view file , you can use it.
$sucessful_message=Message::display();
if($sucessful_message) { ?>
<div id="messagedisplay" class="padding_150">
<div class="notice_message">
<?php echo $sucessful_message; ?>
</div>
</div>
<?php } ?>

Manipulate $session->flash() view output in CakePHP

I am looking to manipulate the $session->flash() output in my CakePHP app, Currently I have the very simple default implementation of showing flash and auth error messages:
<?php
$session->flash();
$session->flash('auth');
?>
This produces a <div> with an ID and class that has the message inside. What I would like to do is wrap/replace the generated HTML, specifically with some jQuery UI classes, but wrapping is difficult as I am unable to tell when there is actually a message going to be displayed so I end up with an empty but style error div. What I really need for wrapping to work is to check in $session->flash() returns anything, but I get 'can't use method return value in write context' when checking it with empty();
As far as I can tell the generated HTML is hard coded into the session helper! Bonus points if you can work out how to change the class on the auth message and normal flash message independently.
To check if a message is going to be flashed, put this in the layout
<?php if($session->check('Message')){ echo $this->Session->flash();} ?>
CSS attributs can be set when you set the message to be flashed
http://book.cakephp.org/view/1311/Methods#setFlash-1313
read up on setFlash() and use the other params that the method takes to define your own elements. you can then do what ever you like.
http://book.cakephp.org/view/400/setFlash
A work-around solution I have found is to set $session->flash() to a variable and then check it with empty() so I can echo out the appropriate <div> if necessary.
$flashMessage = $this->flash();
$authMessage = $this->flash('auth');
.. and then check if each one is empty. Of course I have some unnecessary html inside there but as far as the auth message goes, I think this is as flexible as I can get.

Categories