masudafsar
جمعه 12 اردیبهشت 1393, 20:00 عصر
من یه کلاس usersController دارم ولی نمی دونم چه طوری روال های login و register و logout روو براش بنویسم.
تو سایت cakephp.org هرچی خوندم، چیزی دستگیرم نشد. ورژن کیک هم 2.4 هستش.
کد کنترلر :
<?php
App::uses('AppController', 'Controller');
/**
* CakePHP usersController
* @author Masud Afsar <Masud@Afsar.Info>
*/
class usersController extends AppController {
public $name = "users";
public $uses = array('User');
public function beforeRender() {
$this->set('controller_title', "کاربران");
}
public function index() {
$this->set('action_title', "لیست کاربران");
$options = array(
'conditions' => array(),
'fields' => array(
'User.id',
'User.username',
'User.email',
'User.fullname'
)
);
$users = $this->User->find('all', $options);
$this->set('users', $users);
}
public function add() {
$this->set('action_title', "کاربر جدید");
if ($this->request->is("post")) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash("Add new user successfully", 'admin/flash/success');
// $this->Session->setFlash("Add new user successfully");
$this->redirect(array('controller' => 'users', 'action' => 'index'));
} else {
$this->Session->setFlash("User not saved! please try again.", 'admin/flash/fail');
}
}
}
public function edit($id = NULL) {
$this->set('action_title', "ویرایش کاربر");
if ($this->isValidByID($id, $this->User)) {
if ($this->request->is('post') || $this->request->is('put')) {
if (!isset($this->request->data['User']['password']) || $this->request->data['User']['password'] == "") {
$data = $this->User->findById($this->request->data['User']['id']);
$this->request->data['User']['password'] = $data['User']['password'];
}
if ($this->User->save($this->request->data)) {
$this->Session->setFlash("User updated", 'admin/flash/success');
$this->redirect(array('controller' => 'users', 'action' => 'index'));
} else {
$this->Session->setFlash("User updating fail!", 'admin/flash/fail');
}
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->request->data = $this->User->find('first', $options);
$this->request->data['User']['password'] = NULL;
}
} else {
throw new NotFoundException(__("Invalid user ID"));
}
}
public function delete($id = NULL) {
$this->set('action_title', "حذف کاربر");
if ($this->isValidByID($id, $this->User)) {
if ($this->User->delete($id)) {
$this->Session->setFlash("User deleted", 'admin/flash/warning');
$this->redirect(array('controller' => 'users', 'action' => 'index'));
} else {
$this->Session->setFlash("User deleting fail! please try again.", 'admin/flash/fail');
}
} else {
throw new NotFoundException(__("Invalid user ID"));
}
}
public function profile($id = NULL) {
$this->set('action_title', "پروفایل کاربر");
if ($this->isValidByID($id, $this->User)) {
$options = array(
'conditions' => array(
'User.' . $this->User->primaryKey => $id
),
'fields' => array(
'User.id',
'User.username',
'User.email',
'User.fullname'
)
);
$this->request->data = $this->User->find('first', $options);
} else {
throw new NotFoundException(__("Invalid user ID"));
}
}
public function login() {
$this->layout = "login";
if ($this->request->is("post")) {
$options = array(
'conditions' => array(
'User.username' => $this->request->data['User']['username'],
'User.password' => $this->request->data['User']['password'],
),
'fields' => array(
'User.id',
'User.username',
'User.email',
'User.fullname'
)
);
if ($this->User->find('count', $options) == 1) {
$this->Session->setFlash("Loged in!", "admin/flash/success");
} else {
$this->Session->setFlash("User or pass is wrong!", "admin/flash/warning");
}
}
}
public function logout() {
}
public function register() {
$this->layout = "login";
if ($this->request->is("post")) {
$options = array(
'conditions' => array(
'or' => array(
'User.username' => $this->request->data['User']['username'],
'User.email' => $this->request->data['User']['email'],
)
),
'fields' => array(
'User.id',
'User.username',
'User.email',
'User.fullname'
)
);
if ($this->User->find('count', $options) > 0) {
$this->Session->setFlash("User Exist!", "admin/flash/warning");
} else {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash("Registering is complate. check your email.", "admin/flash/success");
} else {
$this->Session->setFlash("Please try again.", "admin/flash/warning");
}
}
}
}
public function forget() {
$this->layout = "login";
}
}
اگر نیاز به اطلاعات بیشتری دارید لطفا بگید تا بزارم. ممنون.
تو سایت cakephp.org هرچی خوندم، چیزی دستگیرم نشد. ورژن کیک هم 2.4 هستش.
کد کنترلر :
<?php
App::uses('AppController', 'Controller');
/**
* CakePHP usersController
* @author Masud Afsar <Masud@Afsar.Info>
*/
class usersController extends AppController {
public $name = "users";
public $uses = array('User');
public function beforeRender() {
$this->set('controller_title', "کاربران");
}
public function index() {
$this->set('action_title', "لیست کاربران");
$options = array(
'conditions' => array(),
'fields' => array(
'User.id',
'User.username',
'User.email',
'User.fullname'
)
);
$users = $this->User->find('all', $options);
$this->set('users', $users);
}
public function add() {
$this->set('action_title', "کاربر جدید");
if ($this->request->is("post")) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash("Add new user successfully", 'admin/flash/success');
// $this->Session->setFlash("Add new user successfully");
$this->redirect(array('controller' => 'users', 'action' => 'index'));
} else {
$this->Session->setFlash("User not saved! please try again.", 'admin/flash/fail');
}
}
}
public function edit($id = NULL) {
$this->set('action_title', "ویرایش کاربر");
if ($this->isValidByID($id, $this->User)) {
if ($this->request->is('post') || $this->request->is('put')) {
if (!isset($this->request->data['User']['password']) || $this->request->data['User']['password'] == "") {
$data = $this->User->findById($this->request->data['User']['id']);
$this->request->data['User']['password'] = $data['User']['password'];
}
if ($this->User->save($this->request->data)) {
$this->Session->setFlash("User updated", 'admin/flash/success');
$this->redirect(array('controller' => 'users', 'action' => 'index'));
} else {
$this->Session->setFlash("User updating fail!", 'admin/flash/fail');
}
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->request->data = $this->User->find('first', $options);
$this->request->data['User']['password'] = NULL;
}
} else {
throw new NotFoundException(__("Invalid user ID"));
}
}
public function delete($id = NULL) {
$this->set('action_title', "حذف کاربر");
if ($this->isValidByID($id, $this->User)) {
if ($this->User->delete($id)) {
$this->Session->setFlash("User deleted", 'admin/flash/warning');
$this->redirect(array('controller' => 'users', 'action' => 'index'));
} else {
$this->Session->setFlash("User deleting fail! please try again.", 'admin/flash/fail');
}
} else {
throw new NotFoundException(__("Invalid user ID"));
}
}
public function profile($id = NULL) {
$this->set('action_title', "پروفایل کاربر");
if ($this->isValidByID($id, $this->User)) {
$options = array(
'conditions' => array(
'User.' . $this->User->primaryKey => $id
),
'fields' => array(
'User.id',
'User.username',
'User.email',
'User.fullname'
)
);
$this->request->data = $this->User->find('first', $options);
} else {
throw new NotFoundException(__("Invalid user ID"));
}
}
public function login() {
$this->layout = "login";
if ($this->request->is("post")) {
$options = array(
'conditions' => array(
'User.username' => $this->request->data['User']['username'],
'User.password' => $this->request->data['User']['password'],
),
'fields' => array(
'User.id',
'User.username',
'User.email',
'User.fullname'
)
);
if ($this->User->find('count', $options) == 1) {
$this->Session->setFlash("Loged in!", "admin/flash/success");
} else {
$this->Session->setFlash("User or pass is wrong!", "admin/flash/warning");
}
}
}
public function logout() {
}
public function register() {
$this->layout = "login";
if ($this->request->is("post")) {
$options = array(
'conditions' => array(
'or' => array(
'User.username' => $this->request->data['User']['username'],
'User.email' => $this->request->data['User']['email'],
)
),
'fields' => array(
'User.id',
'User.username',
'User.email',
'User.fullname'
)
);
if ($this->User->find('count', $options) > 0) {
$this->Session->setFlash("User Exist!", "admin/flash/warning");
} else {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash("Registering is complate. check your email.", "admin/flash/success");
} else {
$this->Session->setFlash("Please try again.", "admin/flash/warning");
}
}
}
}
public function forget() {
$this->layout = "login";
}
}
اگر نیاز به اطلاعات بیشتری دارید لطفا بگید تا بزارم. ممنون.