Delete user simple link

Hi Guys

I have another page with all usual menu items such as:

Create

Manage

List

View

Update

Delete

everything has a proper link but the Delete. I really dont know how to delete a user.

Thank you

Generally in your view you will need to create a link like this




<?php

echo CHtml::link('delete',array('/controller/delete','id'=>$model->id));

?>



and then in the controller something like this




<?php

public function actionDeleteProfile()

	{

             if(isset($_GET['id']))

                      {

                          $id = $_GET['id'];

                      }  else {

                          throw new CHttpException(400,'Profile not found.');

                      }

		if(Yii::app()->request->isPostRequest)

		{

			// we only allow deletion via POST request

                         $profile = Profile::model()->findByPk($id);

                         $profile->delete();

                 }


	$this->redirect('somewhere/useful');

		}

?>



you might have to delete other records associated with the user record, for example posts, gallery etc.

doodle

Can we have a simple and hopefully simple delete link in view like other options?

For example, that’s what I have for creating a user


 echo CHtml::link('Create',CController::createUrl('user/create')); 

and I have urlManager in config/main to have a clean URL





	'components'=>array(

		

		'urlManager'=>array(

					'urlFormat'=>'path',

					'showScriptName'=>	false,

					'rules'=>array(

							'' => '/site/index',	

							'About' => array('site/page', 'defaultParams' => array('view' => 'about')),

							'Menu' => array('site/page', 'defaultParams' => array('view' => 'menu')),

							'Contact'=>'/site/contact',

							'List'=>'user/index',

							'Create'=>'user/create',

							'Manage'=>'user/admin',

							'login'=>'/site/login',

							'logout'=>'/site/logout',

					

							),

		

		),

...........................




With this code I get a very clean URL for create:




http://localhost/user/Create



On the other hand, I wont need a link like


 http://localhost/user/Delete  

for Delete. All I want is some home have a hyperlink on my view where I can click and it asks me to delete a user. Exactly the same when when GII generated one for me When we have column2 set for in Site controller


 public $layout='//layouts/column2'; 

Let me know if I am not clear.

Thank you :)

@Liz

I’m not any kind of expert with the UrlManager so although I have tried it out, I generally have not been using it. I’m not convinced that clean url’s are worth the hassle.

Please educate me if you have a strong case for using them.

In a nutshell the difference between create and delete is that when you create there is no need to pass the id of the record.

With update and delete you need to let the server know which record you are dealing with. So if you don’t want the id in the url, then maybe you could use Ajax?

To be honest, I’m not quite sure I understand your question.

doodle

Thanks doodle

Yes you are right and we do need the ID for Update and delete so I am kinda hard coding the link for Update to get something like


http://localhost/user/user/update/id/dblevel10

and it is how I generate the link




$theID=Yii::app()->user->name;


echo CHtml::link('Update',CController::createUrl('user/update/id\/').$theID);



which basically generates the above link. Same idea to view a user. I used this:




echo CHtml::link('View',CController::createUrl('user/view/id\/').$theID);



to generate




http://localhost/user/user/view/id/dblevel10



and for urlManager it is all about the client. Some of them do not care but some do care and as end users, they dont want to deal with index.php?r=user\create.

It is just a preference but if the costumer asks for it, you gotta do it :)

Thanks for your help. Am I clear now? I will explain more details if needed.

Thank you again

Check this post

Ya, I stopped suggesting it! :rolleyes:

doodle

I still cannot get the delete link work. I tried to have a menu item called Delete like this:




array('label'=>'Delete(NOT IMPLEMENTED YET)', 'url'=>array('/user/delete/id/'.Yii::app()->user->name))




and was hoping clicking on Delete will delete the current user but apparently, it does not

So what does happen?

What does your controller look like? And are you really trying to make a link that deletes the current user? So if you log in, you can delete yourself?

doodle

What do you need to know about the controller? Then I can show you the code for that part. For now Yes, delete myself then later on I may need a better UI to delete a user if an admin is logged it. Something with a better look than gridView which is a table with Delete/Update/View as just icons.

Thank you

P.S Determining of which user has what access would the topic of another post :)

Well for a start we could maybe look at the delete action, but from my view of what you are trying to do, it seems very illogical.

I assume that you would log the user out before delete?

Have you verified that the link is taking you to the delete action?

Also usually when Gii creates crud for you, delete has to be a post request and not just a simple action.

doodle

So does to me. I have to talk to the client to see why they want it this way.

If you dont mind can I move to next topic I mentioned?

thank you

You should probably start a new topic after reading this.

Check out these extensions

RBAM

RIGHTS

Or anything else found here.

doodle