I was able to use this to redirect to another route and pass data to it as well:
public function method() {
$dir = "A";
$name = "ABC";
return redirect()->route('catch')->with([
'dir' => $dir,
'name' => $name
]);
}
public function catch() {
dd(Session::has('dir'));
dd(Session::has('name'));
}
Above code was working until today
Then I tried setting session and checking within the method:
public function method() {
Session::put('dir', 'a');
Session::put('name', 'abc');
Session::has('dir'); // true
return redirect()->route('catch');
}
public function catch() {
Session::has('dir'); // false
}
(this works because of route Route::get('/catch', ['as' => 'catch', 'uses' => Controller#catch])...)
This was working until a second ago and I was messing up with nginx user permissions, then it stopped working
Related
I'm trying to pass the value of an array from one method to another method in Laravel. Basically I have a method called orderingSubmission that recieves an array from an Ajax POST execution (that part works fine), but then I'm trying to pass the array values to another method called orderingSubmissionReceipt so I can finally render the view on order-summary page. I'm using sessions but they don't seem to work. Can anyone tell me what I'm missing please?
Here's my route.php
Route::get('/', function() { return redirect('home'); });
Route::get('home', 'HomeController#home');
Route::post('home', 'HomeController#activateCustomer');
Route::post('order-summary', 'HomeController#OrderingSubmission');
Route::get('order-summary', 'HomeController#orderingSubmissionReceipt');
HomeController.php
public function orderingSubmission(Request $request)
{
$allValues2 = $request->get('users'); // This code works fine. It gets the array
Log::info('This is orderingSubmission.....: ' . print_r($allValues2, true));
Session::set('allValues2',$allValues2);
}
public function orderingSubmissionReceipt()
{
$allValues3 = Session::get('allValues2'); // This code is not getting the session value
Log::info('This is orderingSubmissionReceipt.....: ' . print_r($allValues3, true));
return view('order-summary', [
'people' => $allValues3
]);
}
You can use $request->session->put() and $request->session->get() as follows : Do not forget to add Request $request in orderingSubmissionReceipt function declaration.
public function orderingSubmission(Request $request)
{
$allValues2 = $request->get('users'); // This code works fine. It gets the array
Log::info('This is orderingSubmission.....: ' . print_r($allValues2, true));
$request->session()->put('allValues2',$allValues2);
}
public function orderingSubmissionReceipt(Request $request)
{
$allValues3 = $request->session()->get('allValues2'); // This code is not getting the session value
Log::info('This is orderingSubmissionReceipt.....: ' . print_r($allValues3, true));
return view('order-summary', [
'people' => $allValues3
]);
}
You can create class variables, like this:
class A
{
private $allValues2 = array();
function orderingSubmission(Request $request)
{
$this->allValues2 = $request->get('users');
//the rest of the code in this method..
}
function orderingSubmissionReceipt()
{
//if you want to make a local variable first, you can do it like this:
$allValues3 = $this->allValues2;
Log::info('This is orderingSubmissionReceipt.....: ' . print_r($allValues3, true));
//or, you can just use the variable directly, like this:
Log::info('This is orderingSubmissionReceipt.....: ' . print_r($this->allValues2, true));
}
}
You can use session() helper method in case Session is not working or showing any error.
public function orderingSubmission(Request $request)
{
$allValues2 = $request->get('users');
session()->set('allValues2', $allValues2);
Log::info('Value set to session: ' . session()->get('allValues2'));
}
public function orderingSubmissionReceipt()
{
Log::info('Value get from session: ' . session()->get('allValues2'));
$allValues3 = session()->get('allValues2');
return view('order-summary', [
'people' => $allValues3
]);
}
You should do something like that:
use Illuminate\Http\Request; //You probably already have that!
public function orderingSubmission(Request $request)
{
$allValues2 = $request->get('users');
$request->session()->put('allValues2', $allValues2);
Log::info('Value set to session: ' . $request->session()->get('allValues2'));
}
public function orderingSubmissionReceipt(Request $request)
{
Log::info('Value get from session: ' . $request->session()->get('allValues2'));
$allValues3 = $request->session()->get('allValues2');
return view('order-summary', [
'people' => $allValues3
]);
}
For more: https://laravel.com/docs/5.1/session
We have this function in helpers.php that returns the lang
function locale() {
if(Cookie::has('locale') && array_key_exists(Cookie::get('locale'), Config::get('languages'))) {
$locale = Cookie::get('locale');
} else {
$locale = 'en';
}
return $locale;
}
On every single page, it works and returns the correct value. But, for some reason, on the index page only, it is always 'en'.
This is how we set it:
function setLocale(Request $request) {
$locale = $request->input('locale');
if(array_key_exists($locale, \Config::get('languages'))) {
$cookie = cookie()->forever('locale', $locale);
\App::setLocale($locale);
}
return redirect()->back()->withCookie($cookie);
}
Whoops, it seems the error was that the home page route was in a group with the web middleware, but we were still setting it
Route::get('/', [
'uses' => 'HomeController#index',
'as' => 'root',
'middleware' => 'web'
]);
removing the middleware from this part since it's already present in the route group solved the issue.
I was given a project to update certain extensions and modules on without any documentation.
The issue I'm having is for one of the variables in the model, it is being regarded as not defined when using localhost (but works fine on the server version which is the exact same code).
From what I can see, it makes a single row in the database and assigns it a value of 1 or 0.
What could be causing this?
Keep in mind, this works fine on the server. I have turned global variables in my php.ini to On. No change. The Row is definitely created and exists in the database with a value. The page errors out after clicking "create new group".
Edit. Error message : Property "EventGroups.delegates_select_event_group " is not defined.
Code below is the model, controller and form. (and the options)
First off, the form view.
echo $form->switchGroup($model, 'delegates_select_event_group', array('class' => 'col-md-6'));
Model
public $delegates_select_event_group;
public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('name, description, guest_invites_per_user, delegates_select_event_group ', 'required'),
);
}
Options
//Options
// Event group selectable
const KEY_DELEGATES_SELECT_EVENT_GROUP = 'delegates_select_event_group';
public static function isDelegatesSelectEventGroup() {
return self::getValue(self::KEY_DELEGATES_SELECT_EVENT_GROUP, false);
}
public static function setDelegatesSelectEventGroup($value) {
return self::setValue(self::KEY_DELEGATES_SELECT_EVENT_GROUP, $value);
}
const KEY_DEFAULT_EVENT_GROUP_ID = 'default_event_group_id';
public static function getDefaultEventGroupID() {
$model = EventGroups::model()->find();
if ($model === null) {
return self::getValue(self::KEY_DEFAULT_EVENT_GROUP_ID, 1);
} else {
return self::getValue(self::KEY_DEFAULT_EVENT_GROUP_ID, $model->id);
}
}
public static function setDefaultEventGroupID($value) {
return self::setValue(self::KEY_DEFAULT_EVENT_GROUP_ID, $value);
}
Controller
public function actionCreate() {
$model = new EventGroups;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if (isset($_POST['EventGroups'])) {
$model->attributes = $_POST['EventGroups'];
if ($model->validate()) {
// Valid Save
$model->save(false);
$this->redirect(array('event/create'));
}
}
$this->render('create', array(
'model' => $model,
));
}
I'm using Socialite to get user information from facebook. All is going well but my redirect isn't working
Sub-routes
I read that it's not possible to do a redirect from a submethod, or
any method that's not in your routes.
But how else can i redirect the user after I logged them in?
My URL looks like this after the successfull facebook handshake
http://tmdb.app/auth/login/facebook?code=AQBTKNZIxbfdBruAJBqZ8xx9Qnz...
Code
class SocialController extends Controller {
public function login(Authenticate $authenticate, Request $request)
{
return $authenticate->execute($request->has('code'), $this);
}
public function userHasLoggedIn($data)
{
$user = User::where('provider_id', $data->id)->first();
if( !$user )
{
$user = User::create([
'name' => $data->name,
'email' => $data->email,
'provider' => 'facebook',
'provider_id' => $data->id
]);
}
// NOT WORKING!
return redirect('test');
}
}
Your login function should be handling the redirect.
I'm guessing execute returns $data if the user is sucessfully logged in and false if not.
class SocialController extends Controller {
public function login(Authenticate $authenticate, Request $request)
{
if($data = $authenticate->execute($request->has('code'), $this))
{
$user = User::where('provider_id', $data->id)->first();
// maybe delegate the user creation to another class/service?
if( !$user )
{
$user = User::create([
'name' => $data->name,
'email' => $data->email,
'provider' => 'facebook',
'provider_id' => $data->id
]);
}
return redirect('test');
}
return redirect('fail_view');
}
}
You can do it using PHP header function in Laravel sub method. I try it and works properly. Hope it can help you.
// You can using the following code
$url= url("about-laravel");
header("Location:" . $url);
exit;
// Or using the following code to redirect and keep set flash message
$result= $this->yourMethod(); // return redirect($this->route)->with('flash_message', 'I\'m Flash Message'); for TRUE or NULL for false
if( $result ){
return $result;
}
I want to override "redirect" to insert a language prefix. For that I've added thist to AppController.php
public function redirect( $url, $status = NULL, $exit = true ) {
die(debug('$url'));
if (!isset($url['language']) && $this->Session->check('Config.language')) {
$url['language'] = $this->Session->read('Config.language');
}
parent::redirect($url,$status,$exit);
}
Obviously the die(..) is just for testing if this function is called.
My controller code:
public function add() {
if ($this->request->is('post')) {
$this->request->data['Party']['language_id'] = $this->Session->read('Config.LanguageId');
$this->Party->create();
if ($this->Party->save($this->request->data)) {
$this->Session->write('partyId', $this->Party->id);
return $this->redirect(array(
'controller' => 'parties',
'action' => 'editGuestlist',
));
} else {
$this->Session->setFlash(__('The party could not be saved. Please, try again.'));
}
}
}
I expected that "$this->redirect" in XyzController will call my public function "redirect" in AppController.
If I'm adding a party I'm getting redirected to /parties/editGuestlist no prefix is added and the code doesn't stop. So it is prety obvious that my overriding is not working. As I'm new to cakephp I'm hoping that somebody can explain this behavior to me.
UPDATE
I've found an error in /config/routes.php that fixed the behavior but I still have no clue why my overriding function isn't called at all.
I've added array('language' => '[a-z]{3}') to
Router::connect('/:language/:controller/:action/*',
array(),
array('language' => '[a-z]{3}')
);