Suyog Rajbhandari
Suyog Rajbhandari

Reputation: 63

How to use the same CRUD operation in frontend and backend in yii2 advance?

I have a table named Userprofile in the backend which saves the user information including profile image Yii::$app->params['uploadPath'] = Yii::$app->basePath . '/web/avatar/'; but when i tried to used the same table from the frontend then the image is saved in the frontend as i have used basePath. How can i save the profile pic in same folder and can do CRUD operation from frontend as well as backend?

Upvotes: 2

Views: 945

Answers (3)

Ram Pukar
Ram Pukar

Reputation: 1621

  1. configuration with migration . folder path: common\config\main-local.php

enter image description here

  1. create table: client
CREATE TABLE `client` (
    `client_id`  int NOT NULL AUTO_INCREMENT ,
    `first_name`  varchar(255) NOT NULL ,
    `middle_name`  varchar(255) NULL ,
    `last_name`  varchar(255) NOT NULL ,
    PRIMARY KEY (`client_id`)
);
  1. go to backend gii example: http://backend.dev/gii

enter image description here

enter image description here

enter image description here

enter image description here

Upvotes: 0

code ghost
code ghost

Reputation: 31

suppose you want to save all images for (frontend and backend) CRUD under frontend/web/uploads so in your backend config file add this component:

'urlManagerFrontEnd' => [
        'class' => 'yii\web\urlManager',
        'baseUrl' => 'http://front.domain.ext/',//frontend app url
], 

then use it from the backend controllers or models :

 Yii::$app->urlManagerFrontEnd->baseUrl ."/path/to/your/place";

Hope this help

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133400

If you need simply render the related views using alias eg:

if you want use a backend view from front end

    class YourControllerController extends Controller
    {
        public function behaviors()
        {
            return [
                'verbs' => [
                    'class' => VerbFilter::className(),
                    'actions' => [
                        'delete' => ['post'],
                    ],
                ],
            ];
        }

        public function actionIndex()
        {
            $searchModel = new YourModelSearch();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
            $dataProvider->pagination->pageSize=15;

            return $this->render('@backend/views/your-controller/index', [
                'searchModel' => $searchModel,
                'dataProvider' => $dataProvider,
            ]);

        }

Upvotes: 1

Related Questions