add path of controller in form->create in Cakephp - cakephp

firstCode
echo $this->Form->create('User', array(
'id' => 'form-login',
'inputDefaults' => array(
'label' => false,
'div' => false,
)
));
Second Code
echo $form->create('User', array('url' => array('controller' => 'users', 'action' => 'login')));
sorry to ask this silly question.i want to merge second code into firstone ..as they are working fine independently.. i have tried many times but failed

You're probably mixing up the arrays. Should all be in one big array (as the create() method only takes 2 arguments), like this:
echo $this->Form->create('User', array(
'url' => array('controller' => 'users', 'action' => 'login'),
'id' => 'form-login',
'inputDefaults' => array('label' => false, 'div' => false)
));

Related

(issue) redirect route cakephp

sorry, my English so bad.
In CakePHP 2.x, I want to redirect old link to new link in my website. But i got some issue.
Example:
mywebsite.com/news/news redirect to mywebsite.com/news
I use:
Router::redirect ('/news/news',
array('controller' => 'categories',
'action' => 'index',
'slug' => 'news'
),
array(
'status' => '301'
)
);
Router::connect('/news', array(
'controller' => 'categories',
'action' => 'index',
'slug' => 'news'
));
=> OK, it work.
But
Example:
mywebsite.com/news/news/a-b-c redirect to mywebsite.com/news/a-b-c
I use:
Router::redirect('/news/news/:slug',
array(
'controller' => 'articles',
'action' => 'view',
'category' => 'news'
),
array(
'status' => '301'
)
);
Router::connect('/news/:slug',
array(
'controller' => 'articles',
'action' => 'view',
'category' => 'news'
)
);
=> result is: mywebsite.com/articles/view/category:news
Please help, thank you !
You are redirecting all requests from news/news/* to articles/view/category:*
Do you need the 301 redirect in there at all?
Router::connect('/news/news/:slug',
array(
'controller' => 'news',
'action' => 'slug'
)
);
But on this basis, the controller will expect a defined action for every type of a-b-c action that can be passed as a slug

Auth->login() function not working properly

Here's the component declaration in AppController.php>
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'User',
'fields' => array('username' => 'email', 'password' => 'code'),
'scope' => array('activated' => true),
),
),
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'loginRedirect' => array('controller' => 'members', 'action' => 'dashboard', 'admin' => true),
'authError' => 'No Permission',
'logoutRedirect' => array('controller' => 'pages', 'action' => 'home'),
'userScope' => array('User.activated' => true),
),
The login form:
<?= $this->Form->create('User', array('url' => '/users/login', 'class' => 'form-inline'));?>
<div class="form-group">
<?= $this->Form->input('User.email', array(
'div' => false,
'label' => false,
'placeholder' => 'е-пошта',
'class' => 'form-control',
'required' => true,
));?>
<?= $this->Form->input('User.code', array(
'div' => false,
'label' => false,
'placeholder' => 'сериски број',
'class' => 'form-control',
'required' => true,
));?>
<?= $this->Form->button('<i class="fa fa-user"></i>', array('type' => 'submit', 'class' => 'btn btn-primary', 'escape' => false));?>
</div>
<?= $this->Form->end();?>
And a snippet of the login function:
// ...
if($this->request->is('post')) {
if($this->Auth->login()) {
if(isset($this->request->data['User']['token']) && $this->request->data['User']['token']) {
$token = substr(md5(time()), 0, 32);
$this->User->id = $this->Auth->user('id');
$this->User->saveField('token', $token);
$this->Cookie->write('remember_me', $token, false, '1 week');
}
return $this->redirect($this->Auth->loginRedirect);
}
// ...
Now, when I use $this->Auth->login($this->request->data) or $this->Auth->login($this->request->data['User']), it works, but when I use only $this->Auth->login() it doesn't. I can do a workaround by logging in with $this->request->data and then putting the rest of the user data manually to be available afterwards, but I want to know why this happens. Any ideas?
EDIT
So, as Karthik Keyan mentioned hashing, i figured this was the problem. CakePHP was automatically hashing the password (code field) and I didn't want it to. So I made a custom hasher class named NoPasswordHasher as follows:
App::uses('AbstractPasswordHasher', 'Controller/Component/Auth');
class NoPasswordHasher extends AbstractPasswordHasher {
public function hash($password) {
return $password;
}
public function check($password, $hashedPassword) {
return $password == $hashedPassword;
}
}
and used it in the Auth component:
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'User',
'fields' => array('username' => 'email', 'password' => 'code'),
'scope' => array('activated' => true),
'passwordHasher' => 'No',
),
),
It works now. Thank you.
Tell what type of errors can be display for you.Please check can you store the password in HASH (salt) format.

Cakephp file upload character corruption

I want to use form to set a user data.
This is my code.It is garbled when enter the Japanese.
However, it works well when i exclude 'type' => 'file'.
Configuration and database is set to utf8 all.
Please give to advice.
echo $this->Form->create('User', array('url' => array('controller' => 'Users', 'action' => 'userprofileedit',$user_id ),'type' => 'file'));
echo $this->Form->input('upfile', array('label' => array('text' => 'upfile'),'type' => 'file'));
echo $this->Form->input('nickname',array('label' => array('text' => 'nickname'),"value"=>$nickname));
echo $this->Form->end(__('Save'));
If you are uploading a file for the User, replace the form creation:
echo $this->Form->create('User', array(
'url' => array('controller' => 'Users', 'action' => 'userprofileedit',$user_id ),
'type' => 'file'
));
with:
echo $this->Form->create('User', array(
'enctype' => 'multipart/form-data'
'url' => array('controller' => 'Users', 'action' => 'userprofileedit',$user_id ),
));
Also make sure that your app/core.php has:
Configure::write('App.encoding', 'UTF-8');

CakePHP Routers with language, slug, pagination

Using CakePHP 1.3 I am trying to get routers with language, slug, pagination, order .
Currently I have these:
Router::connect('/', array('controller' => 'pages', 'action' => 'index'));
Router::connect("/:controller/:slug", array('action' => 'view'), array('pass' => array('slug')));
Router::connect("/:lang", array('controller' => 'pages', 'action' => 'index'), array('lang' => 'fr|en|de'));
Router::connect("/:lang/:controller/:slug", array('action' => 'view'), array('lang' => 'fr|en|de', 'pass' => array('slug')));
and those are working with language and slug set or slug and pagination , but all three of them fails:
OK - /pages/view/page-slug/page:2
OK - /fr/pages/page-slug
FAIL - /pages/view/page-slug/page:2/lang:fr
I have tried
<?php $this->Paginator->options(array('url' => $this->passedArgs)); ?>
before the paginator but still the same result
Just try this code
Router::connectNamed(array('language','pagination','order','slug'));
Router::connect('/lang/pagination/:slug:order', array(
'plugin' => false,
'controller' => 'pages',
'action' => 'index',
),array(
"pass"=>array("lang","pagination","slug","order")
),array(
'pagination' => '[0-9]+',
'order' => '[0-9]+',
)
);

CakePHP 2.x Custom Route with Arguments

In my CakePHP App I'd like to pass arguments in a custom route.
What works now
(domain/controller/action/param)
domain.com/dealers/view/1
What I'd like to do
(domain/param/controller/action/param)
domain.com/washington/dealers/view/1
This is my code in routes.php:
Router::connect('/:city/dealers/view/:id', array('controller' => 'dealers', 'action' => 'view'),
array(
'pass' => array('city', 'id')
),
array('city' => '[a-z]+')
);
This just redirects domain.com/washington/dealers/view/1 to domain.com/dealers/index for the obvious reason that I did not properly pass the parameters. Does anyone know what I am missing?
city should not be in a separate array ex:
Router::connect(
'/:city/dealers/view/:id',
array('controller' => 'dealers', 'action' => 'index'),
array(
'pass' => array('city', 'id'),
'city' => '[a-z]+'
));

Resources