Cakephp does not expire page after logout - session

I am learning cakePHP, I have written the example of the manual, the problem is with the method logout of the UsersController, when I press the link logout the application is redirected to the login form, but the back button of the browser allows to return to a page that requires an authenticated user, an example about of this occurs with the page to add posts
Source Code
UsersController.php
<?php
class UsersController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
// Allow users to register and logout.
$this->Auth->allow('add', 'logout');
}
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(
__('The user could not be saved. Please, try again.')
);
}
}
public function edit($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(
__('The user could not be saved. Please, try again.')
);
} else {
$this->request->data = $this->User->read(null, $id);
unset($this->request->data['User']['password']);
}
}
public function delete($id = null) {
$this->request->onlyAllow('post');
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->User->delete()) {
$this->Session->setFlash(__('User deleted'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('User was not deleted'));
return $this->redirect(array('action' => 'index'));
}
public function login() {
//$this->layout=null;
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->Session->write('userid',$this->Auth->user('id'));
//$this->Session->write('userid',AuthComponent::user('id'));
return $this->redirect($this->Auth->redirect());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
public function logout() {
$this->Session->delete('userid');
$this->Session->destroy();
return $this->redirect($this->Auth->logout());
}
}
?>
PostsController.php
<?php
class PostsController extends AppController {
public $helpers = array('Html', 'Form');
public function isAuthorized($user) {
// All registered users can add posts
if ($this->action === 'add') {
return true;
}
// The owner of a post can edit and delete it
if (in_array($this->action, array('edit', 'delete'))) {
$postId = (int) $this->request->params['pass'][0];
if ($this->Post->isOwnedBy($postId, $user['id'])) {
return true;
}
}
return parent::isAuthorized($user);
}
public function index() {
if ($this->Session->read('userid')) {
$this->set('posts', $this->Post->find('all', array('conditions' => array('Post.user_id' => AuthComponent::user('id')))));
} else {
$this->set('posts', $this->Post->find('all'));
}
}
public function view($id = null) {
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Post->findById($id);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
$this->set('post', $post);
}
public function add() {
if ($this->Auth->loggedIn()) {
if ($this->request->is('post')) {
$this->request->data['Post']['user_id'] = $this->Auth->user('id');
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add your post.'));
}
} else {
return $this->redirect(array('controller' => 'users', 'action' => 'login'));
}
}
public function edit($id = null) {
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Post->findById($id);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
if ($this->request->is(array('post', 'put'))) {
$this->Post->id = $id;
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been updated.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to update your post.'));
}
if (!$this->request->data) {
$this->request->data = $post;
}
}
public function delete($id) {
if ($this->request->is('get')) {
throw new MethodNotAllowedException();
}
if ($this->Post->delete($id)) {
$this->Session->setFlash(
__('The post with id: %s has been deleted.', h($id))
);
return $this->redirect(array('action' => 'index'));
}
}
}
?>
AppController.php
<?php
App::uses('Controller', 'Controller');
/**
* Application Controller
*
* Add your application-wide methods in the class below, your controllers
* will inherit them.
*
* #package app.Controller
* #link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
*/
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'posts', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users','action' => 'login'),
'authorize' => array('Controller') // Added this line
)
);
public function isAuthorized($user) {
// Admin can access every action
if (isset($user['role']) && $user['role'] === 'admin') {
return true;
}
// Default deny
return false;
}
public function beforeFilter() {
$this->Auth->allow('index','view','login','helloajax');
}
}
?>

please check beforeFilter function from your AppController
you have explicitly allowed some action through AuthComponent
public function beforeFilter() {
$this->Auth->allow('index','view','login','helloajax');
}
Please verify actions you want to allow for unauthenticated visitor.
Since AppController is extended by every single controller in cakephp. Which turn out to like you are allowing unauthenticated users to access your index,view,login etc actions for every single controller you have created or will create.

Related

Undefined property: App\Cart::$totalPrice

Hello I am making a cart but when I click on add to cart link then it says:
Undefined property: App\Cart::$totalPrice
Error: https://ibb.co/ysB5CfG
model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Cart
{
private $contents;
private $totalQty;
private $contentsPrice;
public function __construct($oldCart){
if ($oldCart) {
$this->contents = $oldCart->contents;
$this->totalQty = $oldCart->totalQty;
$this->totalPrice = $oldCart->totalPrice;
}
}
public function addProduct($product, $qty){
$products = ['qty' => 0, 'price' => $product->price, 'product' => $product];
if ($this->contents) {
if (array_key_exists($product->slug, $this->contents)) {
$product = $this->contents[$product->slug];
}
}
$products['qty'] +=$qty;
$products['price'] +=$product->price * $product['qty'];
$this->contents[$product->slug] = $product;
$this->totalQty+=$qty;
$this->totalPrice += $product->price;
}
public function getContents()
{
return $this->contents;
}
public function getTotalQty()
{
return $this->totalQty;
}
public function getTotalPrice()
{
return $this->totalPrice;
}
}
controller:
public function cart()
{
if (!Session::has('cart')) {
return view('products.cart');
}
$cart = Session::has('cart');
return view('product.cart', compact('cart'));
}
public function addToCart(Product $product, Request $request, $qty= null)
{
if(empty(Auth::user()->email)){
$data['email'] = '';
}else{
$data['email'] = Auth::user()->email;
}
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$qty = $request->qty ? $request->qty : 1;
$cart = new Cart($oldCart);
$cart->addProduct($product, $qty);
Session::put('cart', $cart);
return redirect()->back()->with('flash_message_success', 'Product $product->title has been successfully added to Cart');
}
routes:
Route::get('cart', 'Admin\ProductController#cart')->name('product.cart');
// Add to cart
Route::get('/addToCart/{product}/{qty?}', 'Admin\ProductController#addToCart')->name('addToCart');
You should use get() methods in cart() function in controller file.
public function cart()
{
if (!Session::has('cart')) {
return view('products.cart');
}
$cart = Session::get('cart');
return view('product.cart', compact('cart'));
}

How to differentiate the multiple panels with login and session?

It create the session but does not go to index2 and index3 always redirect with else and go to index method but i want to go index2 and index3 to handle other panels also.
Session is created successfully for all just comming else condition all the time.
My form data and array is also showing when i using the print_r for my code to view if the data is comming or not.
Problem is it is showing no any error just redirect with file of index method.
My Controller
class Main extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Main_Model');
$this->load->helper('url');
$this->load->library('session');
$method = $this->router->fetch_method();
$methods = array('index','index2','index3');
if(in_array($method,$methods))
{
if(!$this->session->has_userdata('signup_email'))
{
redirect(base_url('Main/login'));
}
}
}
public function index()
{
if($this->session->has_userdata('signup_email'))
{
$this->load->view('BKO/index');
}
}
public function index2()
{
if($this->session->has_userdata('signup_email'))
{
$this->load->view('Admin/index');
}
}
public function index3()
{
if($this->session->has_userdata('signup_email'))
{
$this->load->view('Owner/index');
}
}
public function login()
{
//$data['select'] = $this->Main_Model->get_select();
$this->load->view('login');
}
public function login_process()
{
//$roll = $this->input->post('select');
echo $email = $this->input->post('email');
echo $pass = $this->input->post('upass');
$query = $this->Main_Model->login_process($email,$pass);
if($query == TRUE)
{
$this->session->set_userdata('signup_email');
$session = array(
'signup_email' => $email
);
$this->session->set_userdata($session);
redirect(base_url('Main/check_login'));
}
else
{
$this->session->set_flashdata('error','Invalid Email or Password');
redirect(base_url('Main/login'));
}
}
public function check_login()
{
if($this->session->userdata() == 'admin#gmail.com')
{
echo "Welcome - <h2>".$this->session->userdata('username')."</h2>";
redirect(base_url('Main/index2'));
}
elseif($this->session->userdata() == 'owner#gmail.com')
{
echo "Welcome - <h2>".$this->session->userdata('username')."</h2>";
redirect(base_url('Main/index3'));
}
else
{
echo "Welcome - <h2>".$this->session->userdata('username')."</h2>";
redirect(base_url('Main/index'));
}
}
public function logout()
{
$this->session->sess_destroy();
redirect(base_url());
}
My Model
public function login_process($email,$pass)
{
//$this->db->select('*');
//$this->db->where('roll_id',$roll);
$this->db->where('signup_email',$email);
$this->db->where('signup_password',$pass);
$query = $this->db->get('signup');
if($query->num_rows() > 0)
{
$this->session->set_flashdata('signup_email');
return true;
}
else
{
return false;
}
}
You missed the parameter here
if($this->session->userdata() == 'admin#gmail.com')
instead it should be
if($this->session->userdata('signup_email') == 'admin#gmail.com')

Laravel Redirect to previous dynamic page after login

I wonder how I can redirect a user after login?
Lets say that I am on the page "www.mysite.com/users/2"
Then I try to edit a blog post without being logged in and get sent to the login page, efter login I wish to return to "www.mysite.com/users/2"
I have tried this so far:
if (Auth::attempt($credentials,$remember)) {
return redirect()->back();
} else {
return redirect()->back()->withErrors([trans('api.couldnotlogin')]);
}
But return redirect()->back(); will only redirect me to "www.mysite.com/"
Update
I got it working using this:
public function showLoginForm()
{
$previous_url = Session::get('_previous.url');
$ref = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$ref = rtrim($ref, '/');
if ($previous_url != url('login')) {
Session::put('referrer', $ref);
if ($previous_url == $ref) {
Session::put('url.intended', $ref);
}
}
return view('auth.login');
}
public function loginUser(ApiAuthUserPassRequest $request)
{
if ($request->has('rememberme')) {
$remember = $request->input('rememberme');
} else {
$remember = false;
}
$credentials = ['email' => $request->input('email'), 'password' => $request->input('password')];
if (Auth::attempt($credentials,$remember)) {
if (Session::has('referrer')) {
return redirect()->intended(Session::pull('referrer'));
} else {
return redirect('/account');
}
} else {
return redirect()->back()->withErrors([trans('api.couldnotlogin')]);
}
}
Laravel 5.1 have trait Illuminate/Foundation/Validation/ValidatesRequests.php with method
protected function getRedirectUrl()
{
return app(UrlGenerator::class)->previous();
}
where UrlGenerator is Illuminate/Routing/UrlGenerator.php. You can try use previous() method.

Magento - Hidden value not set in grid serializer

I'm developing an extension for magento which display question groups from custom database table and I need to assign questions(which is loaded from another custom table ), to groups.
So far I could code the grid to display question groups, and questions. When I go to edit one question group, it displays all the questions there in the database. What I need is to select some of them and save. If I load all the questions to one screen ( so far I have 130 questions and I can load all of them to one screen by changing "view 20 per page" to read "view 200 per page" in the grid) and select what I need, and then save, it works as a charm.
But if I select some questions from page 1 and navigate to 2nd page and then select some other questions, and so on, Only the questions in the last page are saved. And all the other selections in the previous pages are lost.
I found that the hidden value of the grid serialize is overridden each time I navigate through pages so selections are lost.
Any advice is greatly appreciate.
Here is the code I used
app\code\community\Muaw\Prescription\Block\Adminhtml\Groups\Edit\Tab\Question.php
class Muaw_Prescription_Block_Adminhtml_Groups_Edit_Tab_Question extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('questionsGrid');
$this->setUseAjax(true);
$this->setDefaultFilter(array('in_questions' => 1));
$this->setSaveParametersInSession(false);
}
protected function _addColumnFilterToCollection($column)
{
if ($column->getId() == 'in_questions') {
$questionIds = $this->_getSelectedQuestions();
if (empty($questionIds)) {
$questionIds = 0;
}
if ($column->getFilter()->getValue()) {
$this->getCollection()->addFieldToFilter('id', array('in' => $questionIds));
} else {
if ($questionIds) {
$this->getCollection()->addFieldToFilter('id', array('nin' => $questionIds));
}
}
} else {
parent::_addColumnFilterToCollection($column);
}
return $this;
}
protected function _prepareCollection()
{
$collection = Mage::getModel('muaw_prescription/question')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('in_questions', array(
'header_css_class' => 'a-center',
'type' => 'checkbox',
'name' => 'in_questions',
'field_name' => 'questions[]',
'values' => $this->_getSelectedQuestions(),
'align' => 'center',
'index' => 'id'
));
$this->addColumn('id', array(
'header' => Mage::helper('catalog')->__('ID'),
'sortable' => true,
'width' => '60',
'index' => 'id'
));
$this->addColumn('question', array(
'header' => $this->__('Question'),
'index' => 'question',
'align' => 'left',
));
return parent::_prepareColumns();
}
protected function _getSelectedQuestions()
{
$customers = array_keys($this->getSelectedQuestions());
return $customers;
}
public function getSelectedQuestions()
{
$tm_id = $this->getRequest()->getParam('id');
if (!isset($tm_id)) {
$tm_id = 0;
}
$questions = array();
$groupq = Mage::getModel('muaw_prescription/qgr')->getCollection()->addFieldToFilter('group_id', $tm_id);
foreach ($groupq as $group) {
$questions[] = $group->getQuestionId();
}
$custIds = array();
foreach ($questions as $cust) {
$custIds[$cust] = array('id' => $cust);
}
return $custIds;
}
public function getGridUrl()
{
return $this->_getData('grid_url') ? $this->_getData('grid_url') : $this->getUrl('*/*/questionsGrid', array('_current' => true));
}
}
app\code\community\Muaw\Prescription\Block\Adminhtml\Groups\Edit\Tabs.php
class Muaw_Prescription_Block_Adminhtml_Groups_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{
public function __construct()
{
parent::__construct();
$this->setId('form_tabs');
$this->setDestElementId('edit_form');
$this->setTitle(Mage::helper('muaw_prescription')->__('Group Information'));
}
protected function _beforeToHtml()
{
$this->addTab('form_section', array(
'label' => Mage::helper('muaw_prescription')->__('Groups Information'),
'title' => Mage::helper('muaw_prescription')->__('Details'),
'content' => $this->getLayout()
->createBlock('muaw_prescription/adminhtml_groups_edit_tab_main')
->toHtml(),
));
$this->addTab('questions', array(
'label' => Mage::helper('muaw_prescription')->__('Associated Questions'),
'url' => $this->getUrl('*/*/questionstab', array('_current' => true)),
'content' => $this->getLayout(),
'class' => 'ajax',
));
return parent::_beforeToHtml();
}
}
app\code\community\Muaw\Prescription\controllers\Adminhtml\GroupsController.php
class Muaw_Prescription_Adminhtml_GroupsController extends Mage_Adminhtml_Controller_Action
{
protected function _initAction()
{
// load layout, set active menu and breadcrumbs
$this->loadLayout()
->_setActiveMenu('prescription/manage')
->_addBreadcrumb(
Mage::helper('muaw_prescription')->__('Group'),
Mage::helper('muaw_prescription')->__('Group')
)
->_addBreadcrumb(
Mage::helper('muaw_prescription')->__('Manage Group'),
Mage::helper('muaw_prescription')->__('Manage Group')
);
return $this;
}
public function indexAction()
{
$this->_title($this->__('Group'))
->_title($this->__('Manage Group'));
$this->_initAction();
$this->renderLayout();
}
public function QuestionAction()
{
$this->_title($this->__('Group Questions'))
->_title($this->__('Manage Group Questions'));
$this->loadLayout()
->_setActiveMenu('groups/manage')
->_addBreadcrumb(
Mage::helper('muaw_prescription')->__('Group Questions'),
Mage::helper('muaw_prescription')->__('Group Questions')
)
->_addBreadcrumb(
Mage::helper('muaw_prescription')->__('Manage Group Questions'),
Mage::helper('muaw_prescription')->__('Manage Group Questions')
);
$this->renderLayout();
}
public function newAction()
{
// the same form is used to create and edit
$this->_forward('edit');
}
public function editAction()
{
$this->_title($this->__('Group'))
->_title($this->__('Manage Group'));
$model = Mage::getModel('muaw_prescription/groups');
$newsId = $this->getRequest()->getParam('id');
if ($newsId) {
$model->load($newsId);
if (!$model->getId()) {
$this->_getSession()->addError(Mage::helper('muaw_prescription')->__('Group does not exist.'));
return $this->_redirect('*/*/');
}
$this->_title($model->getTitle());
$breadCrumb = Mage::helper('muaw_prescription')->__('Edit Item');
} else {
$this->_title(Mage::helper('muaw_prescription')->__('New Item'));
$breadCrumb = Mage::helper('muaw_prescription')->__('New Item');
}
$this->_initAction()->_addBreadcrumb($breadCrumb, $breadCrumb);
$data = Mage::getSingleton('adminhtml/session')->getFormData(true);
if (!empty($data)) {
$model->addData($data);
}
Mage::register('question_item', $model);
$this->renderLayout();
}
public function saveAction()
{
$redirectPath = '*/*';
$redirectParams = array();
// check if data sent
$data = $this->getRequest()->getPost();
if (isset($data['links'])) {
$customers = Mage::helper('adminhtml/js')->decodeGridSerializedInput($data['links']['questions']);
}
//print_r($customers);exit;
if ($data) {
$data = $this->_filterPostData($data);
$model = Mage::getModel('muaw_prescription/groups');
if (empty($data['pid'])) {
$arr = array();
$arr['form_key'] = $data['form_key'];
$arr['name'] = $data['name'];
$model->addData($arr);
$out = $model->save()->getId();
} else {
$arr = array();
$arr['form_key'] = $data['form_key'];
$arr['id'] = $data['pid'];
$arr['name'] = $data['name'];
$model->load($data['pid'])->addData($arr);
$model->setId($data['pid'])->save();
$out = $data['pid'];
}
try {
$hasError = false;
$this->_getSession()->addSuccess(Mage::helper('muaw_prescription')->__('The Group has been saved.'));
// check if 'Save and Continue'
if ($this->getRequest()->getParam('back')) {
$redirectPath = '*/*/edit';
$redirectParams = array('id' => $out);
}
$groupq = Mage::getModel('muaw_prescription/qgr')->getCollection()->addFieldToFilter('group_id', $out);
$sel_question = array();
foreach ($groupq as $group) {
if (!empty($group->getQuestionId())) {
$sel_question[$group->getId()] = $group->getQuestionId();
}
}
$del_arr = $new_arr = array();
$del_arr = array_diff($sel_question, $data['questions']);
$new_arr = array_diff($data['questions'], $sel_question);
if (!empty($data['questions'])) {
if (!empty($del_arr)) {
foreach ($del_arr as $del => $val) {
$id = $del;
$model_qgr = Mage::getModel('muaw_prescription/qgr');
try {
$model_qgr->setId($id)->delete();
} catch (Exception $e) {
//echo $e->getMessage();
}
}
}
} else {
$collection = Mage::getModel('muaw_prescription/qgr')->getCollection()->addFieldToFilter('group_id', $out);
foreach ($collection as $item) {
$id = $item->getId();
$model_qgr = Mage::getModel('muaw_prescription/qgr');
try {
$model_qgr->setId($id)->delete();
} catch (Exception $e) {
//echo $e->getMessage();
}
}
}
if (!empty($new_arr)) {
foreach ($new_arr as $new) {
if ($new != 'on') {
$new_data = array();
$new_data['question_id'] = $new;
$new_data['group_id'] = $out;
try {
$model_qgr = Mage::getModel('muaw_prescription/qgr')->setData($new_data);
$insertId = $model_qgr->save()->getId();
} catch (Exception $e) {
//echo $e->getMessage();
}
}
}
} else {
}
} catch (Mage_Core_Exception $e) {
$hasError = true;
$this->_getSession()->addError($e->getMessage());
} catch (Exception $e) {
$hasError = true;
$this->_getSession()->addException($e, Mage::helper('muaw_prescription')->__('An error occurred while saving the Group.'));
}
if ($hasError) {
$this->_getSession()->setFormData($data);
$redirectPath = '*/*/edit';
$redirectParams = array('id' => $data['pid']);
}
}
$this->_redirect($redirectPath, $redirectParams);
}
public function deleteAction()
{
// check if we know what should be deleted
$itemId = $this->getRequest()->getParam('id');
if ($itemId) {
try {
$model = Mage::getModel('muaw_prescription/groups');
$model->load($itemId);
if (!$model->getId()) {
Mage::throwException(Mage::helper('muaw_prescription')->__('Unable to find a group.'));
}
$model->delete();
$this->_getSession()->addSuccess(
Mage::helper('muaw_prescription')->__('The group has been deleted.')
);
} catch (Mage_Core_Exception $e) {
$this->_getSession()->addError($e->getMessage());
} catch (Exception $e) {
$this->_getSession()->addException($e,
Mage::helper('muaw_prescription')->__('An error occurred while deleting the group.')
);
}
}
$this->_redirect('*/*/');
}
protected function _initGroups()
{
$groupModel = Mage::getModel('muaw_prescription/groups');
$groupId = (int)$this->getRequest()->getParam('id', 0);
if ($groupId) {
try {
$groupModel->load($groupId);
if (!$groupModel->getId()) {
throw new Exception($this->__('This group no longer exists'));
}
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
return null;
}
}
Mage::register('current_group', $groupModel);
return $groupModel;
}
protected function _isAllowed()
{
switch ($this->getRequest()->getActionName()) {
case 'new':
case 'save':
return Mage::getSingleton('admin/session')->isAllowed('groups/manage/save');
break;
case 'delete':
return Mage::getSingleton('admin/session')->isAllowed('groups/manage/delete');
break;
default:
return Mage::getSingleton('admin/session')->isAllowed('groups/manage');
break;
}
}
protected function _filterPostData($data)
{
$data = $this->_filterDates($data, array('time_published'));
return $data;
}
public function gridAction()
{
$this->loadLayout();
$this->renderLayout();
}
protected function _isAjax()
{
if ($this->getRequest()->isXmlHttpRequest()) {
return true;
}
if ($this->getRequest()->getParam('ajax') || $this->getRequest()->getParam('isAjax')) {
return true;
}
return false;
}
public function questionsTabAction()
{
$itemId = $this->getRequest()->getParam('id');
$groupq = Mage::getModel('muaw_prescription/qgr')->getCollection()->addFieldToFilter('group_id', $itemId);
$sel_question = array();
foreach ($groupq as $group) {
if (!empty($group->getQuestionId())) {
$sel_question[] = $group->getQuestionId();
}
}
$saved_question_ids = $sel_question; // your load logic here
if (!empty($saved_question_ids)) {
$this->loadLayout()
->getLayout()
->getBlock('muaw.tab.questions')
->setQuestions($saved_question_ids);
} else {
$this->loadLayout()
->getLayout()
->getBlock('muaw.tab.questions')
->setQuestions($this->getRequest()->getPost('questions', null));
}
$this->renderLayout();
}
public function questionsGridAction()
{
$this->loadLayout()
->getLayout()
->getBlock('muaw.tab.questions')
->setQuestions($this->getRequest()->getPost('questions', null));
$this->renderLayout();
}
public function questionsAction()
{
$this->loadLayout();
$this->getLayout()->getBlock('muaw.tab.questions')
->setQuestions($this->getRequest()->getPost('questions', null));
$this->renderLayout();
}
}
app\design\adminhtml\default\default\layout\muaw_prescription.xml
<adminhtml_groups_edit>
<update handle="editor"/>
<reference name="left">
<block type="muaw_prescription/adminhtml_groups_edit_tabs" name="groups.edit.tabs" />
</reference>
<reference name="content">
<block type="muaw_prescription/adminhtml_groups_edit" name="groups.edit" />
</reference>
</adminhtml_groups_edit>
<adminhtml_groups_questionstab>
<block type="core/text_list" name="root" output="toHtml">
<block type="muaw_prescription/adminhtml_groups_edit_tab_question" name="muaw.tab.questions"/>
<block type="adminhtml/widget_grid_serializer" name="muaw.serializer.questions">
<reference name="muaw.serializer.questions">
<action method="initSerializerBlock">
<grid_block_name>muaw.tab.questions</grid_block_name>
<data_callback>getSelectedQuestions</data_callback>
<hidden_input_name>links[questions]</hidden_input_name>
<reload_param_name>questions</reload_param_name>
</action>
<action method="addColumnInputName">
<input_name>position</input_name>
</action>
</reference>
</block>
</block>
</adminhtml_groups_questionstab>
<adminhtml_groups_questionsgrid>
<block type="core/text_list" name="root">
<block type="muaw_prescription/adminhtml_groups_edit_tab_question" name="muaw.tab.questions"/>
</block>
</adminhtml_groups_questionsgrid>

This webpage has a redirect loop in Laravel 4

I'm busy with a tutorial and I ended up getting an error that says
This webpage has a redirect loop
I know that the problem is here in my routes.php
Route::group(["before" => "guest"], function(){
$resources = Resource::where("secure", false)->get();
foreach($resources as $resource){
Route::any($resource->pattern, [
"as" => $resource->name,
"uses" => $resource->target
]);
}
});
Route::group(["before" => "auth"], function(){
$resources = Resource::where("secure", true)->get();
foreach($resources as $resource){
Route::any($resource->pattern, [
"as" => $resource->name,
"uses" => $resource->target
]);
}
});
UserController
class UserController extends \BaseController {
public function login()
{
if($this->isPostRequest())
{
$validator = $this->getLoginValidator();
if($validator->passes())
{
$credentials = $this->getLoginCredentials();
if(Auth::attempt($credentials)){
return Redirect::route("user/profile");
}
return Redirect::back()->withErrors([
"password" => ["Credentials invalid."]
]);
}else{
return Redirect::back()
->withInput()
->withErrors($validator);
}
}
return View::make("user/login");
}
protected function isPostRequest()
{
return Input::server("REQUEST_METHOD") == "POST";
}
protected function getLoginValidator()
{
return Validator::make(Input::all(), [
"username" => "required",
"password" => "required"
]);
}
protected function getLoginCredentials()
{
return [
"username" => Input::get("username"),
"password" => Input::get("password")
];
}
public function profile()
{
return View::make("user/profile");
}
public function request()
{
if($this->isPostRequest()){
$response = $this->getPasswordRemindResponse();
if($this->isInvalidUser($response)){
return Redirect::back()
->withInput()
->with("error", Lang::get($response));
}
return Redirect::back()
->with("status", Lang::get($response));
}
return View::make("user/request");
}
protected function getPasswordRemindResponse()
{
return Password::remind(Input::only("email"));
}
protected function isInvalidUser($response)
{
return $response === Password::INVALID_USER;
}
public function reset($token)
{
if($this->isPostRequest()){
$credentials = Input::only(
"email",
"password",
"password_confirmation"
) + compact("token");
$response = $this->resetPassword($credentials);
if($response === Password::PASSWORD_RESET){
return Redirect::route("user/profile");
}
return Redirect::back()
->withInput()
->with("error", Lang::get($response));
}
return View::make("user/reset", compact("token"));
}
protected function resetPassword($credentials)
{
return Password::reset($credentials, function($user, $pass){
$user->password = Hash::make($pass);
$user->save();
});
}
public function logout()
{
Auth::logout();
return Redirect::route("user/login");
}
}
GroupController
class GroupController extends \BaseController {
public function indexAction()
{
return View::make("group/index", [
"groups" => Group::all()
]);
}
public function addAction()
{
$form = new GroupForm();
if($form->isPosted()){
if($form->isValidForAdd()){
Group::create([
"name" => Input::get("name")
]);
return Redirect::route("group/index");
}
return Redirect::route("group/add")->withInput([
"name" => Input::get("name"),
"errors" => $form->getErrors()
]);
}
return View::make("group/add", [
"form" => $form
]);
}
public function editAction()
{
$form = new GroupForm();
$group = Group::findOrFail(Input::get("id"));
$url = URL::full();
if($form->isPosted()){
if($form->isValidForEdit()){
$group->name = Input::get("name");
$group->save();
$group->users()->sync(Input::get("user_id", []));
$group->resources()->sync(Input::get("resource_id", []));
return Redirect::route("group/index");
}
return Redirect::to($url)->withInput([
"name" => Input::get("name"),
"errors" => $form->getErrors(),
"url" => $url
]);
}
return View::make("group/edit", [
"form" => $form,
"group" => $group,
"users" => User::all(),
"resources" => Resource::where("secure", true)->get()
]);
}
public function deleteAction()
{
$form = new GroupForm();
if($form->isValidForDelete()){
$group = Group::findOrFail(Input::get("id"));
$group->delete();
}
return Redirect::route("group/index");
}
}
but I'm not sure how to go about fixing it especially since I was following a tutorial.

Resources