How To Map A Url To A Module Without The Module Name In The Path

Hi, I was wondering if anyone could help me with a small problem I am having with URLs

I have several modules which are used largely as stand alone applications (admin is on a different domain for security, multiple websites etc). What I want to do that I can’t seem to figure out is map a module to a URL.

For example let’s say I have www.admindomain.com and a module named “admin”, I need to use a path like admindomain.com/admin/controllerID/actionID. What I would like to do is map all requests to admindomain.com to remove the need for /admin.

I got most of the way with URL rules but ran into some problems:

I use multiple different action parameters and sometimes more than one so my rules have to include every variation which is not useful - I would like Yii’s default handling just to work and assume /admin. I have specific error handlers for each module and this means if an erroneous path does not conform to one of Yii’s rules for the module then the default error handler is called.

I have tried mapping all requests to admindomain.com to /admin with a rule like:

"http://www.admindomain.com/(.*)" => "admin/" (or something like this)

But it did not work. Can anyone tell me is there a way to make yii work as normal but just assume a module path when on this domain - if I could get that it would solve a lot of problems.

Thanks for any help you guys can give.

Hi Callum,

you may try this i have also implement this

first you can defind the protectd/config/main.php


'urlManager'=>array(

                        'urlFormat'=>'path',

                        //'showScriptName'=>false,

                        'rules'=>array(

                                'admin/'	        =>'admin/index/index',

                                'admin/login'		=>'admin/index/login',

                                'admin/logout'		=>'admin/index/logout',

                                'admin/<controller:\w+>/<action:\w+>'=>'admin/<controller>/<action>',




                                'customer/'		=>'customer/index/index',

                                'customer/login'	=>'customer/login',

                                'customer/logout'	=>'customer/index/logout',

                                'customer/<controller:\w+>/<action:\w+>'=>'customer/<controller>/<action>',

                

                  		'vendor/'=>'vendor/index/index',

                                'vendor/login'	=>'vendor/login',

                                'vendor/logout'	=>'vendor/index/logout',

                                'vendor/<controller:\w+>/<action:\w+>'=>'vendor/<controller>/<action>',




                        

                ),

in my every module i will set a defult action on index and default controller index so i will set the defaultController on config/main.php file


'defaultController'=>'index/index',

if you run the admin you may write on brower address field

Back-End Url => www.test.com/admin

Front-End Url => www.test.com

i hope you got my point.

Thanks Ankit for the reply but I think you may have missed my goal here.

I have a working solution now that I just need to test and improve. For anyone else stuck with this or anyone who can improve this here is what I have done:

Specified a urlmanager rule as a custom class




'rules' => array(

    'class' => 'path.to.AdminUrlRule',

),



In my Admin Url rule I have:




class AdminUrlRule extends CBaseUrlRule

{

    public function createUrl($manager, $route, $params, $ampersand)

    {

        return false;

        // Haven't tackled this yet

    }


    public function parseUrl($manager, $request, $pathInfo, $rawPathInfo)

    {

        // Get the requested domain

        $domain = strtolower($request->getHostInfo());


        if(strpos($domain, 'admindomain.com')) {

            // Return the path with "/admin" prepended to it

            return '/admin' . rtrim('/' . $pathInfo, '/') . '/';

        } else {

            // Admin domain not requested so don't use this rule

            return false;

        }

    }

}



This means that the request comes out of the class with the path adjusted for the module and then Yii continues as normal. What I need to do here is refactor this to allow different modules and figure out the createUrl method. Any opinions are welcome.

1 Like