Yii2 Links Between Frontend and Backend (Advanced Template)

Yii2 Links between Frontend and Backend (advanced template)

In your backend application config you should add additional 'UrlManager' component with different name and configuration equals to the one used at front end application:

return [
'components' => [
'urlManager' => [
// here is your backend URL rules
],
'urlManagerFrontEnd' => [
'class' => 'yii\web\urlManager',
'baseUrl' => '/a/frontend/web',
'enablePrettyUrl' => true,
'showScriptName' => false,
],

],
];

Then you should invoke following to compose front-end URL:

Yii::$app->urlManagerFrontEnd->createUrl();

Yii2 advanced, when to use the frontend folder and when the backend folder?

Its verry easy.

Backend is your admin page, and frontend is your user page.

Let me explain.

You have models, what your backend use, and frontend use. If both have to use, you have to use common folder to share models with back and frontend.

When you make an app, in frontend you can create everything, what user need. Like registration, login, about us, etc.

In backend this will be your admin page. Like user search, modify rows, etc.

If any of your model need to user, and to admin, like user table, you have to use common folder. Then frontend, and backend will see this model. Be carefull with roules.

I hope i helped. :)

Yii2 Advanced Template use model from frontend in console

use app\models\Mymodelname;

Have not worked with this include statement before. I prefer to use the frontend/backend as is. If I had to make a wild guess though, this looks for models within console. @app is an alias for current application.

Usually, I keep anything used by more than 1 application under common. As you pointed out, maintaining two copies of the same code is not a good practice.

Try using frontend\models\Mymodelname directly if moving to common is not an option

getting Yii2 backend webroot url from frontend root?

In your frontend config/main.php You can do it add a urlManagerBackend entry ..

'components' => [
//can name it whatever you want as it is custom
'urlManagerBackend' => [
'class' => 'yii\web\urlManager',
'baseUrl' => 'mybackendend/absolutepath/uploads/',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
],

and the you can refer to this in this way

Yii::$app->urlManagerBackend->baseUrl;

You can see this form yii2 forum

Yii 2 Advanced Template url

The advance template consist of 3 diff apps frontend, backend and console. Where console is for the Cli and the rest for web, it isnt like basic-app, so you need to access it via http://localhost/yii/advanced/frontend/web/index.php or http://localhost/yii/advanced/backend/web/index.php

Two frontend and one backend in one app Yii Framework 2

You can build as many fronts as you need by adding them as independent modules. Each within a separate folder, with its own configs and own controllers. Then they may all share the same models.

You may check Module docs for more details or you may also check this example which is built for REST on top of a basic template, so you'll need different apache and app configs but it is practically the same.



Related Topics



Leave a reply



Submit