Action Names From Database

Is there possibility to create action names from database. For eg. I want to have menu system that will be stored in database (id:1, name:news ; id:2, name:contact…), and depending on what link user clicks I’ll have site content (that is also stored in the database).

So instead user have to type: somesite.com/menu/show/1, I would like to be able to request for somesite.com/menu/news, or just somesite.com/news.

Is there some easy way to implement this?

You should be able to extend CWebApplication and override it’s processRequest() method. See the source of that method in the CWebApplication.php file. You can put that class into protected/components (check that you import this path in your main.php). Then in index.php you create and run your application like this:


Yii::createApplication('MyDbWebApplication',$config)->run();

Did you take a look at the URL management chapter in the guide? It’ll do what you want. http://www.yiiframework.com/doc/guide/topics.url

Maybe i misunderstood. I thought you try to save your own URL rules in DB and use them to resolve an URL to a route.

You could use the missingAction function in CController and take the the action and and do what you want like look it up in a database. Here’s how I used it to create an ajax application path where the actions are stored in the application’s file and I don’t have to manually update it when i add new apps


	public function missingAction($actionID)

	{

		// build the full app name. This prevents someone from getting soemthing else

		// included

                $app = 'plazl'.ucfirst(strtolower($actionID)).'App';

		$request = Yii::app()->getRequest();

		//jsonp callback

                $callback = $request->getParam('callback', '');

		

		// non secure connections must use xmlhttprequest's secure connections will allowed

		// to use normal fetches due to cross site scripting however we'll make so that

		// the apps have to specfically state they will use a secure connection

		if(!$request->isAjaxRequest && !$request->isSecureConnection)

		{

			header('Content-Type: application/json');

			if($callback != '')

				echo $callback."({errorStr: 'Applications must use XMLHttpRequest', error: 1})";

			else

				echo "{errorStr: 'Applications must use XMLHttpRequest', error: 1}";

			die();

		}


		$this->validateUserToken($request->getQuery('token', ''));


		// test to see if the app file exists in our two app directories

		if(!$this->testAppExists($app))

		{

			header('Content-Type: application/json');

			if($callback != '')

			echo $callback."({errorStr: 'Application: ".$actionID." does not exist', error: 2})";

			else

			echo "{errorStr: 'Application: ".$actionID." does not exist', error: 2}";

			die();

		}


		$appObj = new $app;

		// we do this so we have the exact capitialization of the class name for the form

		$app = get_class($appObj);

		if(!$appObj->usesSecureConnections() && $request->isSecureConnection)

		{

			header('Content-Type: application/json');

			if($callback != '')

				echo $callback."({errorStr: 'Application does not allow secure connections', error: 5})";

			else

			echo "{errorStr: 'Application does not allow secure connections', error: 5}";

			die();

					}

		$appAction = ucfirst(strtolower($request->getQuery('appAction', '')));

		if($appAction == '' || !$appObj->validAction('action'.$appAction))

		{

			header('Content-Type: application/json');

			if($callback != '')

				echo $callback."({errorStr: 'Application action: ".$appAction." is not valid', error: 3})";

			else

				echo "{errorStr: 'Application action: ".$appAction." is not valid', error: 3}";

			die();

		}

		// the action can have a pre action to set things up

		// doesn't have to exist

		$appActionFull = 'preAction'.$appAction;

		if($appObj->validaction($appActionFull))

		{

			if(!$appObj->$appActionFull($this)) die();

		}


		if(isset($_POST[$app]))

		{

			$appObj->attributes=$_POST[$app];

		}


		// do the actual action.

		$appActionFull = 'action'.$appAction;

		$appObj->$appActionFull($this);


		// the action can also have a post action if it needs one

		$appActionFull = 'postAction'.$appAction;

		if($appObj->validaction($appActionFull))

		{

			$appObj->$appActionFull($this);

		}

	}