How to pass values throuh link in Zend Framework? - php

I want to pass values through
<a href= <?php $this->baseUrl()/admin/registration/activate/> </a>
this is my code an i have to pass user Id to this url to activate user. what I have to use I wll get my user id <?php $this->userid; ?>
please help
my action is
public function activateAction()
{ // Administrator actvate user
$user_name = $this->getRequest()->getParam('user_name');
$reg = new clmsRegistrationModel();
$reg->setActive($user_name);
}

inside your action
$this->_request->getParam('prop_id', null);

Lets assume you have Controller Index and Action Customer, so you can build you link with an Zend Framework View helper (inside your view script):
echo $this->url(array(
'controller' => 'index',
'action' => 'customer',
'prop_id' => 5
));
in your Controller:
customerAction() {
$propId = $this->getRequest()->getParam('prop_id');
}
After your comment (add to your view script):
<?php $postUrl = $this->url(array(
'controller' => 'admin',
'action' => 'registration',
'activate' => $this->userid
)); ?>
Activate

Related

Change Default action base on user groups in CakePHP

I have different users groups e.g. admin, author,publisher and have separately controllers for them I want to set default path after login on base of
$this->Auth->User('group_id')
like this in appcontroller in beforefilter() method
if ($this->Auth->User('group_id') == '1')
{
Router::connect('/', array('controller' => 'admin', 'action' => 'index'));
}
elseif($this->Auth->User('group_id') == '2')
{
Router::connect('/', array('controller' => 'author', 'action' => 'index'));
}
else {
Router::connect('/', array('controller' => 'publisher', 'action' => 'index'));
}
I tried this in
routes.php
in Config using $_SESSION Variable because in that file couldnt use
$this.
Main purpose is that when user login it will take to their controller and so i can have clean URL
I dont want to same controller i have to use check for group than ACL library's power will be in waste.
Any Help will be appreciated to accomplish this. Thanks in advance
Based on your comment on Isaac's answer, you can do something like this.
Say, in routes.php you can have:
Route::connect('/', array('controller' => 'some_controller', 'action' => 'index'));
Then in your redirected controller's index() method:
public function index()
{
$group = $this->User->Group->find('first', array( //assuming User belongsTo Group
'conditions' => array(
'id' => $this->Auth->User('group_id')
),
'fields' => array('name')
)
)); //getting the name of the group the user belongs to
call_user_func(array($this, strtolower($group['Group']['name']).'_index'));
}
And then in your controller you can have something like:
protected function admin_index()
{
//code for admins
}
protected function publisher_index()
{
//code for publishers
}
protected function author_index()
{
//code for authors
}
So you have all code on the same controller but separated on different methods.
//Don't miss the app_controller for this small kind of thing. bear in mind always keep the app controller clean and tidy.
function login(){
//here you can say after login what's happen next
if ($this->Auth->login()){
$this->redirectUser();
}
}
//redirect user groups
function redirectUser(){
$role = $this->Auth->User('group_id');
switch ($role) {
case 1:
return $this->redirect(array('controllers'=>'admin_controller', 'action'=>'action_name'));
break;
case 2:
return $this->redirect(array('controllers'=>'author_controller', 'action'=>'action_name'));
default:
return $this->redirect(array('controllers'=>'user_controller', 'action'=>'action_name'));
break;
}
}
If you want to use custom url You also need to name it into routes.php
Route::connect('/admin', array('controller' => 'admin_controller', 'action' => 'index'));
the rest of links

How to use different model from the controller?

I am trying to insert record using Cakephp.My model name is something like User.php.
And My working controller name is SignupsController.I want to insert record using this two but I cant.I am give my some codes below :
View :
<?php echo $this->Form->create('Signups',array('action' => 'registration'));?>
<div class="row-fluid">
<div class="span5">
<label class="">*First Name</label>
<?php echo $this->Form->input('first_name', array('type' => 'text','label' => false, 'class' => 'input-xlarge validate[required]', 'div' => false)); ?>
</div>
<div class="span5">
<label class="">*Last Name</label>
<?php echo $this->Form->input('last_name', array('type' => 'text', 'label' => false, 'class' => 'input-xlarge validate[required]', 'div' => false)); ?>
</div>
</div>
<?php echo $this->Form->end(); ?>
My controller code is given below :
class SignupsController extends AppController {
var $name = 'Signups';
var $uses=array("User");
public function registration()
{
$this->layout="reserved";
$this->Club->create();
if (isset($_POST['registration'])) {
echo "This is";
echo "<pre>";print_r($this->request->data);echo"</pre>";
$this->User->save($this->request->data);
//$this->Session->setFlash(__('Promoter registration has been done successfully'));
//$this->redirect('registration');
//$this->redirect(array('action' => 'registration'));
}
}
}
My model name is different which's name is User.php
I want to insert the record using this above code.Any idea how to insert?
you can do this by loading the users model in current controller just write the following line
$this->loadModel('Name of the Model').
then
$this->nameofmodel->save()
As you are unable to understand see this
Controller::loadModel(string $modelClass, mixed $id)¶
The loadModel() function comes handy when you need to use a model which is not the controller’s default model or its associated model:
$this->loadModel('Article');
$recentArticles = $this->Article->find(
'all',
array('limit' => 5, 'order' => 'Article.created DESC')
);
$this->loadModel('User', 2);
$user = $this->User->read();
Above pasted code is taken from CookBook of Cakephp, if you still do not understand just read it it has complete detailed explanation you can also see this to understand
you can use it with $uses variable in SignupController
class SingupController extends AppController
{
public $uses = array('User');
//rest of stuff
}
Or, if you want, you can load it on-demand inside a method:
$this->loadModel('User'); //now model is loaded inside controller and used via $this->User
EDIT: Your data array has to include the name of the model you're saving. So, replace:
$this->Form->create('Signups',array('action' => 'registration')
with:
$this->Form->create('User',array('url' => array('controller' => 'signups', 'action' => 'registration'));

Yii How to render Users page using Id in model, but username instead of ID in URL

I have Users pages.
In index view they all have hyperlinks on ID, and so they are sending ID to model.
And when u press the link the users View is loaded and the url becomes
/site/users/1
But what I want is the URL to be not users ID but users Username like
/site/users/Alex
How can I do it ?
This is my _view
<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>
Controller
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}
You will need to modify the following files :-
main.php(config)
"urlManager" => array(
"rules" => array(
"/site/users/<name:[\w\_]>" => "/site/users/view",
//All the other rules here...
)
)
Controller
public function actionView($name){
$mdoel = User::model()->findByAttributes(array(
"username" => $name
));
if($model){
$this->render("view", array(
"model" => $model
));
}
}
And wherever you are putting a link, make the link as follows :-
<?php echo CHtml::link(CHtml::encode($data->id), array("view", "name" => $data->username)); ?>
Update :-
Try using the following syntax :-
<?php echo Chtml::link(CHtml::encode($data->id), $this->createUrl('view', array('name'=>$model->username))); ?>

Why won't my flash message show? (Cake PHP 2.0)

In AppController:
public $helpers=array("Session","Html","Form");
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'MainPages', 'action' => 'home'),
'logoutRedirect' => array('controller' => 'MainPages', 'action' => 'front')
)
);
In MainPagesController:
public function front()
{
$this->Session->setFlash('Your stuff has been saved.');
debug($this->Session->read('Message'));
//etc...
In default layout (default.ctp)
<div id="content">
<?php echo "Flash:" ?>
<?php echo $this->Session->flash(); ?>
The correct flash message shows in the debug but not on the view. What am I missing? PS It's not because of space after the ?>.
Edit: I have discovered that CakePHP is calling the session helper before the session component for some reason. Still trying to figure out how to fix it.
Simple way to create flash messages is to create their ctp file in app/view/element dir
Try
<div id="content">
<?php echo "Flash:" ?>
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Session->flash(); ?>
You need to define flash('auth') in your view to see authentication session flash messages.
My setflash works in this way..
Keep this in the App controller
function setFlash($message, $type = 'blue') {
//what ever the color you need..
$arr = array('red' => 'red', 'green' => 'green', 'yellow' => 'yellow', 'gray' => 'gray', 'blue' => 'blue');
$this->Session->setFlash($message, 'default', compact('class'), $arr[$type]);
}
and then call it from the controller action where you need to make the flash message as below..
$this -> setFlash("This is flash message","red");
now you need to make the flash in the layout(if you are using) or in your ctp file..
<?php echo $this->Session->flash() ?>
<?php echo $this->Session->flash('red') ?>
I think it is because you have an error here:
<?php echo "Flash:" ?>
You are missing the semi-colon
<?php echo "Flash:"; ?>

CakePHP 2.0+: Retrieving POST data from Js->submit form in controller

I am using $this->Js->submit to pass a value to my controller asynchronously and than update a div (id = #upcoming). Somehow I cannot save/retrieve the value of the field 'test' which is passed to my controller. Firebug tells me that the correct value is passed. What am I doing wrong?
View file (playlist.ctp):
echo $this->Form->create('Add', array('url' => array('controller' => 'Gods', 'action' => 'add')));
echo $this->Form->input('test');
echo $this->Js->submit('Addddd', array(
'url' => array(
'controller' => 'Gods',
'action' => 'add'
),
'update' => '#upcoming'
));
echo $this->Form->end();
echo $this->Js->writeBuffer(array('inline' => 'true'));
Controller action:
public function add()
{
$this->autoLayout = false;
$this->layout = 'ajax';
$link = $this->request->data['Add']['test'];
$this->set('test',$link);
}
And its view file (add.ctp):
<?php
echo $test;
?>
Thanks for your help!
have you tried pr($link) in the controller method? Or just send it to a log file if you prefer that. That way you can see if the data is received.
If so, I think there is nothing returned because of
$this->autoLayout = false;
Try it without this. It will still call the ajax layout instead of the default.
Otherwise you have to manualy call the render function
$this->render('add');
EDIT
As explained in the comments below, make sure your views are in the right place. (that would be the view folder associated with the controller)

Categories