1

I have installed yii2 on my system. The default url is localhost/projectname/backend/web/.

I want my url should be http://localhost/projectname for frontend and http://localhost/projectname/admin for backend. I am following the 2nd answer i.e given by despotbg of this link Yii2 htaccess - How to hide frontend/web and backend/web COMPLETELY but I am getting the following error

Invalid Call – yii\base\InvalidCallException Setting read-only property: yii\web\Application::request

please suggest me how can i remove this error so that i can rewrite my project url. I am working on wamp server

Community
  • 1
  • 1
Akhilesh Jha
  • 158
  • 1
  • 10
  • This most likely has nothing to do with yii but to do with your virtual host setup. I would ask this question on the wamp questions page. – dataskills Jun 03 '15 at 18:54
  • I cleaned up your syntax some (wrapping your example URLs lets other people know it's not a link) but you might want to post some more code details so other readers can help you. I also tagged this as wamp per your question and the other comment. – Machavity Jun 03 '15 at 19:58

2 Answers2

1

Answer was Updated And now work fine...

Frontend

Modify file frontend/config/main.php:

....
    'components' => [
        ....
        'request'=>[
            'baseUrl'=>'/projectname',
        ],
        'urlManager'=>[
            'scriptUrl'=>'/projectname/index.php',
        ],
        // use the following, if you want to enable speaking URL for the frontend
//        'urlManager' => [
//            'enablePrettyUrl' => true,
//            'showScriptName' => false,
//        ],
    ],

Backend

Modify file backend/config/main.php:

....
    'components' => [
        ....
        'request'=>[
            'baseUrl'=>'/projectname/admin',
        ],
        'urlManager'=>[
            'scriptUrl'=>'/projectname/admin/index.php',
        ],
        // use the following, if you want to enable speaking URL for the backend
//        'urlManager' => [
//            'enablePrettyUrl' => true,
//            'showScriptName' => false,
//        ],
    ],

Apache (.htaccess with mod_rewrite)

Create a file .htaccess in the project root directory (where composer.json is):

RewriteEngine On

# End the processing, if a rewrite already occurred
RewriteRule ^(frontend|backend)/web/ - [L]

# Handle the case of backend, skip ([S=1]) the following rule, if current matched
RewriteRule ^admin(/(.*))?$ backend/web/$2 [S=1]

# handle the case of frontend
RewriteRule .* frontend/web/$0

# Uncomment the following, if you want speaking URL
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^([^/]+/web)/.*$ $1/index.php

source

AliLotfi
  • 360
  • 2
  • 13
0

backend\config\main.php

// backend, under components array
         'request'=>[
             'class' => 'common\components\Request',
             'web'=> '/backend/web',
             'adminUrl' => '/admin'
         ],
         'urlManager' => [
                 'enablePrettyUrl' => true,
                 'showScriptName' => false,
         ],

create request.php file on common\components

<?php

namespace common\components;


class Request extends \yii\web\Request {
    public $web;
    public $adminUrl;

    public function getBaseUrl(){
        return str_replace($this->web, "", parent::getBaseUrl()) . $this->adminUrl;
    }


    /*
        If you don't have this function, the admin site will 404 if you leave off 
        the trailing slash.

        E.g.:

        Wouldn't work:
        site.com/admin

        Would work:
        site.com/admin/

        Using this function, both will work.
    */
    public function resolvePathInfo(){
        if($this->getUrl() === $this->adminUrl){
            return "";
        }else{
            return parent::resolvePathInfo();
        }
    }
}

Try this i hope this will help you..

Nodemon
  • 946
  • 1
  • 21
  • 44