Fatal error: Cannot unset string offsets in AuthComponent.php on line 781 - cakephp-2.5

Error: Cannot unset string offsets
File: /var/www/lib/Cake/Controller/Component/AuthComponent.php
Line: 781
Code:
if (isset($this->request->data['User'])) {
$this->Auth->login();
}
Any idea why this error happens?

Inside the AppController.php
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'pages',
'action' => 'display',
'home'
),
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display',
'home'
),
'authError' => 'You must be logged in to view this page.',
'loginError' => 'Invalid Username or Password.',
'authenticate' => array(
'passwordHasher' => 'Blowfish',
)
)
);
authenticate needs to be
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish',
)
)

Related

Routing in the Zend Framework 2

It's not long since I'm using Zend Framework 2 and I'm trying to write a route for a product that have the rule something like that:
/[slug]-test[ID][av|ai|cv|ci|svb|sib|svc|sic|svi|sii|tv|ti]
An exemple for this rule would look like this:
/freezer-in-good-shape-test12345cv
I try to this with REGEX:
'details' => array(
'type' => 'Zend\Mvc\Router\Http\Regex',
'options' => array(
'route' => '(?<slug>[a-zA-Z0-9_-]+)-test(?<id>[0-9]+)(?<sufix>(av|ai|cv|ci|svb|sib|svc|sic|svi|sii|tv|ti))',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'List',
'action' => 'details',
),
'spec' => '%slug%-test%id%%sufix%',
)
),
and with SEGMENT too:
'details' => array(
'type' => 'Segment',
'options' => array(
'route' => '/:slug-test:id:sufix',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'List',
'action' => 'details',
)
'constraints' => array(
'slug' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]*',
'sufix' => '[av|ai|cv|ci|svb|sib|svc|sic|svi|sii|tv|ti]'
)
)
),
but it's not working. Can anyone tell what is that i'm doing wrong? Thanks!
I’d go with Segment here. The only issue I’ve noticed in your route config is the sufix constraint: instead of [av|ai|…] you should type (av|ai|…).
please try with this-
'details' => array(
'type' => 'Segment',
'options' => array(
'route' => '/:slug[-test]:id:sufix',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'List',
'action' => 'details',
)
'constraints' => array(
'slug' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]*',
'sufix' => '[av|ai|cv|ci|svb|sib|svc|sic|svi|sii|tv|ti]'
)
)
),
on this line 'route' => '/:slug-test:id:sufix',
you had the issues, as it considers "slug-test" as single variable.
so for this you should separate "-test" with "slug" variable.
'route' => '/:slug[-test]:id:sufix',

Error url not work when create a navigation in Zend 2

In module.config.php
'router' => array(
'routes' => array(
'blog' => array(
'type' => 'Literal',
'options' => array(
'route' => '/page',
'defaults' => array(
'controller' => 'Blog\Controller\List',
'action' => 'index'
)
),
'may_terminate' => true,
'child_routes' => array(
'detail' => array(
'type' => 'Segment',
'options' => array(
'route' => '/:id',
'defaults' => array(
'action' => 'detail'
),
'constraints' => array(
'id' => '[0-9]+'
)
)
)
)
)
)
),
...
'navigation' => array(
'default' => array(
array(
'label' => 'Home',
'route' => 'home',
),
array(
'label' => 'About Us',
'route' => 'blog',
'params' => array('id' => '1'),
),
),
),
...
And the ListController.php
public function detailAction()
{
$id = $this->params()->fromRoute('id');
try {
$post = $this->postService->findPost($id);
} catch (\InvalidArgumentException $ex) {
return $this->redirect()->toRoute('blog');
}
return new ViewModel(array(
'post' => $post
));
}
View:
<?php echo $this->navigation('navigation')->menu()->setUlClass('menu menu-home')->escapeLabels(false); ?>
Result only show
<ul class="menu menu-home">
<li class="active">
Home
</li>
<li>
About Us
</li>
</ul>
How to fix this route url, to show url http://domain.com/page/1
I think you are referencing the parent route blog instead of the child route blog/detail. So even though you specified a param it'll get ignored as the parent route does not expect/need a param. Try this:
array(
'label' => 'About Us',
'route' => 'blog/detail', // instead of 'blog'
'params' => array('id' => '1'),
),

cakephp form validation password comparision

I have a function to reset a password.
At the point where I have to compare the password and the password_confirm field, I get a strange behavior:
My form
echo $this->Form->hidden('tkn', array('value' => $tkn));
echo $this->Form->hidden('uid', array('value' => $uid));
echo $this->Form->input('password',array('type' => 'password', 'name' => 'data[Appuser][password]'));
echo $this->Form->input('password_confirm',array('type' => 'password', 'name' => 'data[Appuser][password_confirm]'));
My model validation:
var $validate = array(
'password' => array(
'rule' => array('minLength', '6'),
'message' => '{password} minLength 6!'
),
'password_confirm' => array(
'rule' => array('equaltofield','password'),
'message' => '{password_confirm} not equal!'
),
);
function equaltofield($val1, $val2){
return $this->data[$this->alias][key($val1)] == $this->data[$this->alias][$val2];
}
My Controller:
if($this->Appuser->save($this->data)){
$this->Session->setFlash(__('Password has been updated'));
}else{
debug($this->Appuser->invalidFields());
}
Now:
When I submit an empty form I get the following returned from the invalidFields()
array(
'password' => '*****',
'password_confirm' => array(
(int) 0 => '{password_confirm} not equal!',
(int) 1 => '{password_confirm} not equal!'
)
)
Question 1: Why do I not get the message, that the password has not its minlength?
Question 2: Why do I get the second message twice for comparing the password?
When typing in 2 different password with min length, I get this again:
array(
'password_confirm' => array(
(int) 0 => '{password_confirm} not equal!',
(int) 1 => '{password_confirm} not equal!'
)
)
When debug($this->data) I also get this (if that helps somehow)
array(
'Api' => array(
'tkn' => '6837d241bf1076c3c55a95abbcfafa04dc19a33c',
'uid' => '1'
),
'Appuser' => array(
'password' => '*****',
'password_confirm' => 'asdfgh'
)
)
Any ideas regarding my two questions above?
Thanks in advance!!
Try this
var $validate = array(
'password' => array(
'rule' => array('minLength', '6'),
'message' => '{password} minLength 6!'
),
'password_confirm' => array(
'rule' => 'equaltofield',
'message' => '{password_confirm} not equal!'
),
);
function equaltofield($data){
return $this->data[$this->alias]['password'] == $this->data[$this->alias]['password_confirm'];
}

How to redirect with GET query in Zend 2?

I can redirect to matchable routes like $this->redirect()->toRoute('controller', array('action' => 'something', 'foo' => 'bar')), but how can I append a get query? I'm using it for a filter for a table. The filter can either be submitted on the page (form get method), or linked from another page (need to add get query).
try this :
'course' => array(
'type' => 'Segment',
'options' => array(
'route' => '/course[/:action[/:id]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '\d+'
),
'defaults' => array(
'__NAMESPACE__' => 'Home\Controller',
'controller' => 'course',
'action' => 'index',
'id' => 0
),
),
'may_terminate' => true,
'child_routes' => array(
'query' => array(
'type' => 'Query',
'options' => array(
'route' => '?[:filterByDepartment]'
),
),
),
),
make sure the route is met your need in the view :
echo $this->url('course/query', array('controller'=>'course', 'action'=>'index', 'id'=>0, 'filterByDepartment'=>'depA'));
if this is right, let's try the following in the controller action :
$this->redirect()->toRoute('course/query', array('controller'=>'course', 'action'=>'index', 'id'=>0, 'filterByDepartment'=>'depA'));

Zend_Router requirements mismatch

I have two routes that match a url with the same apparent pattern, the difference lies in the $actionRoute, this should only be matched if the variable :action on it equals 'myaction'.
If I go to /en/mypage/whatever/myaction it goes as expected through $actionRoute.
If I go to /en/mypage/whatever/blahblah it gets rejected by $actionRoute and matched by $genRoute.
If I go to /en/mypage/whatever it should be matched by $genRoute but it gets matched by $actionRoute instead throwing and exception because the action noactionAction() does not exist.
Don't know what I'm doing wrong, I'd appreciate your help.
$genRoute = new Zend_Controller_Router_Route(
':lang/mypage/:var1/:var2',
array(
'lang' => '',
'module' => 'mymodule',
'controller' => 'index',
'action' => 'index',
'var1' => 'noone',
'var2' => 'no'
),
array(
'var1' => '[a-z\-]+?',
'lang' => '(es|en|fr|de){1}'
)
);
$actionRoute = new Zend_Controller_Router_Route(
':lang/mypage/:var1/:action',
array(
'lang' => '',
'module' => 'mymodule',
'controller' => 'index',
'action' => 'noaction',
'var1' => 'noone',
),
array(
'action' => '(myaction)+?',
'var' => '[a-z\-]+?',
'lang' => '(es|en|fr|de){1}',
)
);
$router->addRoute('genroute',$genRoute);
$router->addRoute('actionroute',$actionRoute);
By providing a default value ('noaction') for action in your $actionRoute, you are making that variable optional. If you remove this it should all work fine. Also the 'var' key in the regexp patterns in your second route should probably be 'var1' as it is in your first.
So, you probably want your second route to be:
$actionRoute = new Zend_Controller_Router_Route(
':lang/mypage/:var1/:action',
array(
'lang' => '',
'module' => 'mymodule',
'controller' => 'index',
'var1' => 'noone',
),
array(
'action' => '(myaction)+?',
'var1' => '[a-z\-]+?',
'lang' => '(es|en|fr|de){1}',
)
);