Customizing a URL in the advanced template

When working with multiple applications in the same project, you might require access from an application to another, for example, from the backend to a frontend link. This is because we want to display public page rendering in the frontend after inserting data in the backend.

The urlManager property is customized with references about the application where it is defined. However, we can add specific properties to refer to the respective application.

Therefore, in common/config/main.php, we can add these two properties:

        'urlManagerFrontend' => [
            'class' => 'yiiweburlManager',
            'baseUrl' => '/yiiadv/frontend/web',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
        ],      
        
        'urlManagerBackend' => [
            'class' => 'yiiweburlManager',
            'baseUrl' => '/yiiadv/backend/web',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
        ],

For example, we can get a URL to frontend from everywhere. It is enough to write this code echo Yii::$app->urlManagerFrontend->createUrl(...) to create a URL from frontend.

Note

It is necessary to put the .htaccess file in the web folder of each application that has the enablePrettyUrl property in the urlManager configuration.

Yii also provides convenient aliases to application paths, other than the default aliases of the basic template:

  • @common: This is the common directory
  • @frontend: This is the frontend web application directory
  • @backend: This is the backend web application directory
  • @console: This is the console directory

Example – using the advanced template in the same domain

We have seen that the advanced template creates more applications in the same web application than we can reach using /frontend or /backend or any other application name prefix in the URL. However, it is not advisable, especially for frontend, that all URLs contain a /frontend prefix.

We want to have this URL format for frontend: http://hostname/yiiadv/; and this one for backend: http://hostname/yiiadv/admin (we can choose the name we want).

All requests have to be managed on the /yiiadv folder level. So, we will add an .htaccess file in the /yiiadv folder that it will dispatch to the correct route.

Here is a list of the actions that must be performed:

  1. Configure .htaccess in /yiiadv to handle all requests.
  2. Configure the backend application to customize its baseUrl.
  3. Configure the frontend application to customize its baseUrl.

It is obvious that steps 2 and 3 must be repeated for any other application, for which we want to manipulate the base URL.

For step 1, let's put the .htaccess file with the following content in the /yiiadv folder:

RewriteEngine on
# For Backend
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/yiiadv/admin
RewriteRule ^admin(/.+)?$ /yiiadv/backend/web/$1 [L,PT]
# For Frontend
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /yiiadv/frontend/web/$1

Therefore, in the Backend block of .htaccess, we catch requests in /yiiadv/admin and redirect them to the yiiadv/backend/web/ base URL.

For step 2, the backend requests capture is completed when we also make these changes in backend configuration, adding the request property in backend/config/main.php:

        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => '2OofX7Q9e-EQLSK5BEk70_07fUXkka8y',
            'baseUrl' => '/yiiadv/admin',     
        ],

Now, point the browser to http://hostname/yiiadv/admin and if we did everything correctly we should finally be able to see the login page.

Note

Make sure there is a request attribute in the configuration array in backend/config/main-local.php; we need to comment this otherwise it will overwrite request in the backend/config/main.php file that we have just changed.

Finally, just like we have done with backend requests, in step 3, we need to change the request property for frontend requests under frontend/config/main.php in the configuration:

        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'ear8GcRjBGXQgKVwfEpbApyj7Fb0UKXk',
            'baseUrl' => '/yiiadv',     
        ],

Now, point the browser to http://hostname/yiiadv and if we did everything correctly, we should see the congratulation page of the frontend.

As the last part of this example, if we want to reach the frontend to the http://hostname URL and backend to the http://hostname/admin URL, we must put an .htaccess file in the document root folder with this content:

RewriteEngine on
# For Backend
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^admin(/.+)?$ /yiiadv/backend/web/$1 [L,PT]
# For Frontend
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /yiiadv/frontend/web/$1

Then, we must change the request property of the frontend configuration in frontend/config/main.php with:

        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'ear8GcRjBGXQgKVwfEpbApyj7Fb0UKXk',
            'baseUrl' => '',     
        ],

Finally, change the request property of the backend configuration in backend/config/main.php with:

        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => '2OofX7Q9e-EQLSK5BEk70_07fUXkka8y',
            'baseUrl' => '/admin',     
        ],

In this way, the frontend is now reachable pointing the browser to http://hostname and the backend to http://hostname/admin.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.179.119