How to use the backend's models in the frontend?

My team developed a backend (content administration) based on Yii and my job is to create a frontend (public site) to go along with it. I tried going along with this method on the wiki (http://www.yiiframework.com/wiki/33/) but they show how to use the frontend’s models for the backend, and I have to do the opposite. I tried to adapt the method to my needs, but it doesn’t seem to be working.

How do you think I should proceed to make a frontend for an already built Yii-based backend?

Hi if you they have developed the back-end as a module then you have load the module models in your main.php or if they use a typical approach then you can just access the models in your controllers as following


$records = ModelName::model()->findAll();

// pass this $records to you view and then in you view you can iterate over it 

foreach($records as $record) {

  echo $record->fieldname

}

Yii::import(‘path.to.model’)? :)

Or extend one from an existing model.

Happy coding.

why do you have to access the backend ? don’t think it ’ s a good idea B)

it’ s like low level user want access the high level user 's resource .

or you should define your owner model(even there exists in your backend ).

anyway you can access it by Yii::import(‘backend.models.xxx’) ; you can define the “backend” as alias point to the backend dir . or use the “webroot” alias to access.

but remember if there exist reference (use another alias in your models ) that can cause alias collision or alias can’t find :lol:

hi,

You can also import them in your frontend Module file with




public function init()

	{

             $this->setImport(array(

			'BackendModuleNmae.models.*',

		));

         .....



If the model is to be used for the frontend as well, maybe it doesn’t belong in the backend and should be moved to the models folders in the root application.

If you really need to keep the model in the backend module, you could write a function in the module that can be called by the frontend, instead of directly accessing the model.

The application should work whether the module is there or not.

Actually I think I didn’t phrase it correctly; The backend that was created is a content management system targeted for the site’s administrators. Users will use the frontend, placed at the web directory’s root, and administrators will use the backend to manage the content, accessed throught the /admin directory. The database and the models the site uses should obviously be the same for the front and backend. The views however will be different. The frontend shows the data to the users while the backend manages it through the hands of the administrators.

Maybe I use the terms “frontend” and “backend” a bit loosely too. By “frontend” I mean what the user can see on the site, and by “backend” I mean the software the administrators use to manage the site’s content.

Anyway, I think I managed to make it work, by setting ‘basePath’ to the backend directory and importing ‘backend.models.’ and 'backend.controllers.’ in the frontend’s main.php file. I don’t know if it’s such a good practice though.