Codeigniter basic delete - codeigniter

I have a link that calls this function:
function delete($id)
{
//Delete from database
$this->db->delete('messages', array('id' => $id));
$data['delete_message'] = 'Message was successfully deleted';
redirect('admin');
}
As you can see I redirect to the admin function, and I want to pass the delete_message to that function. How can I do this?

Have you looked at "Flashdata"?. You can set your success message in the flash and the next page (admin in your case) reads it if available and passes it to the view as a regular $data['foo'].

Related

Laravel not redirects page after sending mail

I am sending a mail and after mail is send I want to redirect user to a specific url
$mail = Mail::send('test.mail', ['a' => 'a'], function (Message $message) {
$message->to(['hod#vu.edu.pk', 'student#vu.edu.pk',]);
$message->from('stms#vu.edu.pk');
$message->subject('Test Mail');
});
// dd($mail);
return response()->redirectToRoute('allocate_supervisor')
->with('message', 'Supervisor Assigned and sent to HoD for approval.');
I have tried to return redirect()->route(), url(), and redirect('allocate_supervisor') but each time same issue.
dd($mail); works fine and shows output but on redirect it shows blank page.
Also request status code is 200.
Also tried
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
before return still no output
There are several ways to generate a RedirectResponse instance. The simplest method is to use the global redirect helper:
return redirect('/allocate_supervisor');
make sure 'allocate_supervisor' route must be present in your web.php file.
if you have named routes, you may use route() method.
return redirect()->route('/allocate_supervisor');
I was calling storePhase function from store function and returning response from storePhase function which was being overridden in store function and I was receiving blank page...
I moved this response code to store function and it worked fine.

Session data not saved in plugin controller October CMS

In October CMS on the next request the session does not contain data previously set.
What I did is:
I am trying to use an action method of a plugin controller in October CMS to put data in the session.
\Session::put('name', 'Test Name');
\Session::keep(['name']);
var_dump(\Session::get('name'));
After that I redirect to a specific page of my application
return \Redirect::to('/created');
In this page a component is loaded which is supposed to display data.
However when I try to access the session data in the back-end of this component
var_dump(\Session::get('name')); echo '<br>';
the session does not contain the data I'd put previously at all.
I already
Checked whether the '/storage/framework/sessions' folder is writable.
Whether the session has started.
Checked whether there are no other requests between the controller action and the next page (component).
Set a longer session lifetime.
Checked whether it is 'the correct' session and it is since it contains data set by middleware.
Also tried to add flash messages with both:
session()->flash("message", "Registered successfully");
or
\Flash::success('Settings successfully saved!');
or
return \Redirect::to('/created')->with('message', 'Registered successfully');
I have my controller class in the following folder structure:
Plugin controller
And the action method looks as following:
public function create(\HttpRequest $request)
{
// When robot
if($request->input("recaptcha") != "success") {
// Sets a successful message
session()->flash("message", "Registered successfully");
session()->flash("status", "Success");
session()->flash("alert-class", "alert-success");
\Session::put('name', 'Test Name');
}
return \Redirect::to('/created');
}
Can anybody help?
I know this response is late, but I had the exact same issue and after many attempts, I found out the issue. You have to group your routes inside web middleware as follows:
Route::group(['middleware' => ['web']], function () {
// your routes here
});

How can we redirect to previous page after login in cibonfire?

How can we redirect to previous page after login in cibonfire?
If some module require login to view, than the code there will check for login and if it is not logged in, it will redirect to login page. After login we want to get back to same module. How can we do so.
Thanks
Before your checking ,A user is already loged in ,Add
$this->session->set_userdata('page_url', current_url());
Add in your login function ,After checking the login data.
if($this->session->userdata('page_url'))
redirect($this->session->userdata('page_url'));
else
redirect('default home page');
At the end of your controller function add this line
redirect('your_view_here', $any_variables_here);
I use sessions for logging in and for navegation tasks like that, because it just makes life a lot simpler. In my controller functions I drop in a line of code like the following:
$this->load_segs(func_get_args(), strtolower(__CLASS__).'/'.__FUNCTION__);
Here's the load segements function:
function load_segs($params, $src){
if ( ! is_array($params) || ! isset($src)){
return FALSE;
}
foreach ($params as $key => $value){
//only include values that don't need encoding
if ($value != urlencode($value)){
break;
}
$src .= '/'.htmlentities($value, ENT_QUOTES, 'UTF-8');
}
$this->session->set_userdata('return', $src);
return $src;
}
You can retrieve the information as follows:
$return = html_entity_decode($this->session->userdata('return'));
redirect($return);

How can I trigger controllerless core Magento modules that watch <controller_action_predispatch> in <frontend> in config.xml from external script

For background reference
SEE: Magento: How do I get observers to work in an external script?
I wanted to ask what the preferred method to 'replicate' a frontend controller's action from an external script is. I am creating an external SSO login for Magento EE 1.12.
My code exists as the following in a php file. You can test it by creating test.php and replacing my user (185) with whatever your user ID is. Navigate to the page and then again. You will notice you are logged in and out, however in admin it will not show you as being online. Read on...
<?php
umask(0);
require_once 'app/Mage.php';
Mage::app("default");
// Set the session to frontend according to many suggestions
Mage::getSingleton("core/session", array("name" => "frontend"));
// Alan Storm suggestion to load specific area in Magento (frontend)
Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND);
// Load My Specific User
$user = Mage::getModel('customer/customer')->load(185)->setWebsiteId(Mage::app()->getStore()->getWebsiteId());
// Get the session object
$session = Mage::getSingleton('customer/session');
// Make it look good for debugging
echo "<PRE>";
// Toggle the customer logged in / logged out upon page load
if ($session->isLoggedIn())
{
try
{
$session->session->setCustomer($user);
echo "LOGGED IN<br>";
var_dump($session->getCustomer()->getIsJustConfirmed());
} catch (Mage_Core_Exception $e) {
$message = $e->getMessage();
var_dump($message);
}
} else {
$session->logout();
}
var_dump($session->getCustomer());
echo $session->isLoggedIn() ? $user->getName().' is online!' : 'not logged in';
?>
This code logs in the user, however none of the Mage_Log, Mage_Persistent, or any other module without a controller that relies on the controller_action_predispatch and controller_action_postdispatch event attached to the frontend area in config.xml will ever fire.
Mage_Log is a perfect example of this situation where it watches customer_login and fires the bindCustomerLogin() function (since I'm using Alan Storm's suggestion above) but the controller dispatch does not fire, resulting in failure of the module to work properly.
How can these other modules ever possibly be triggered from an external script (or a global observer watching the controller_front_init_routers event)?
EDIT: SOLUTION
Here is the final results from the suggestions by benmarks above... I am emulating the Mage_Customer controller. The script below demonstrates how to perform a COMPLETE magento login. It is not extensively tested, but it does show the user as being logged in in the backend. It is the most complete solution i've seen to date.
public function autoMageLogin() {
// Include the controller itself so the object can be used
include ('/var/www/app/code/core/Mage/Customer/controllers/AccountController.php');
// Configure Magento to think its using the frontend
Mage::getSingleton("core/session", array("name" => "frontend"));
Mage::getConfig()->init();
Mage::getConfig()->loadEventObservers('frontend');
Mage::app()->addEventArea('frontend');
Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND);
// Prepare the request as if it were coming from the specific
// controller (I've chosed Mage_Customer as my chosen controller
// to 'emulate' in php code
$request = Mage::app()->getRequest();
$request->setRouteName('customer');
$request->setControllerModule('Mage_Customer');
$request->setRoutingInfo('');
$request->setModuleName('customer');
$request->setControllerName('account');
$request->setModuleKey('module');
$request->setControllerKey('account');
$request->setActionName('loginPost');
$request->setActionKey('action');
$response = Mage::app()->getResponse();
// Instantiate a new AccountController object using the modified request
// and the modified response (see the include() above)
$accountControl = new Mage_Customer_AccountController($request, $response);
// Dispatch events associated to the controller
Mage::dispatchEvent('controller_action_predispatch', array('controller_action' => $accountControl));
Mage::dispatchEvent('controller_action_predispatch_customer', array('controller_action' => $accountControl));
Mage::dispatchEvent('controller_action_predispatch_customer_account_loginPost', array('controller_action' => $accountControl));
// Load the current user
$user = Mage::getModel('customer/customer')->load(185)->setWebsiteId(Mage::app()->getStore()->getWebsiteId());
// Grab the current session
$session = Mage::getSingleton('customer/session');
// From this point forward, emulate the controller actions
if (!$session->isLoggedIn()){
try{
$session->setCustomerAsLoggedIn($user);
$session->renewSession();
echo "LOGGED IN<br>";
} catch (Mage_Core_Exception $e) {
$message = $e->getMessage();
var_dump($message);
}
} else {
echo "LOGGED OUT<br>";
$session->logout();
}
// Now fire the post controller action events
Mage::dispatchEvent('controller_action_postdispatch_customer_account_loginPost', array('controller_action'=>$accountControl));
Mage::dispatchEvent('controller_action_postdispatch_customer', array('controller_action'=>$accountControl));
Mage::dispatchEvent('controller_action_postdispatch', array('controller_action'=>$accountControl));
// log to the screen and be done
var_dump($session->getCustomer());
echo $session->isLoggedIn() ? $session->getCustomer()->getName().' is online!' : 'not logged in';
die();
}
You will need to manually dispatch the events with the original params e.g.
Mage::dispatchEvent(
'controller_action_predispatch',
array('controller_action',Mage::app()->getRequest()
);
See Mage_Core_Controller_Varien_Action::preDispatch() (link) for more info. Note that the pre- and post-dispatch methods dispatch dynamic events based on routename params which may or may not be a concern.
If you rewrite the external script as a custom controller and action then all the events will fire naturally. However, you must have a reason for making it external in the first place.

Redirect to show_404 in Codeigniter from a partial view

I am using the HMVC pattern in CodeIgniter. I have a controller that loads 2 modules via modules::run() function and a parameter is passed to each module.
If either module cannot match the passed paramter I want to call show_404(). It works, but it loads the full HTML of the error page within my existing template so the HTML breaks and looks terrible. I think I want it to redirect to the error page so it doesn't run the 2nd module. Is there some way to do that and not change the URL?
Is it possible to just redirect to show_404() from the module without changing the URL?
Here is an over simplified example of what's going on:
www.example.com/users/profile/usernamehere
The url calls this function in the users controller:
function profile($username)
{
echo modules::run('user/details', $username);
echo modules::run('user/friends', $username);
}
Which run these modules, which find out if user exists or not:
function details($username)
{
$details = lookup_user($username);
if($details == 'User Not Found')
show_404(); // This seems to display within the template when what I want is for it to redirect
else
$this->load->view('details', $details);
}
function friends($username)
{
$details = lookup_user($username);
if($friends == 'User Not Found')
show_404(); // Redundant, I know, just added for this example
else
$this->load->view('friends', $friends);
}
I imagine there is just a better way to go at it, but I am not seeing it. Any ideas?
You could throw an exception if there was an error in a submodule and catch this in your controller where you would do show_404() then.
Controller:
function profile($username)
{
try{
$out = modules::run('user/details', $username);
$out .= modules::run('user/friends', $username);
echo $out;
}
catch(Exception $e){
show_404();
}
}
Submodule:
function details($username)
{
$details = lookup_user($username);
if($details == 'User Not Found')
throw new Exception();
else
// Added third parameter as true to be able to return the data, instead of outputting it directly.
return $this->load->view('details', $details,true);
}
function friends($username)
{
$details = lookup_user($username);
if($friends == 'User Not Found')
throw new Exception();
else
return $this->load->view('friends', $friends,true);
}
You can use this function to redirect 404 not found page.
if ( ! file_exists('application/search/'.$page.'.php')) {
show_404(); // redirect to 404 page
}
its very simple , i solved the problem
please controler name's first letter must be capital e.g
A controller with
pages should be Pages
and also save cotroler file with same name Pages.php not pages.php
also same for model class
enjoy

Resources