How to create URLs to other applications

Nope. I don’t think it’s the “right thing to do”

If you need some information from the other app, you might want to expose an API, so that the apps can be anywhere and written in anything.

You can write the back-end in Django - it’s famous for it’s auto-admin interface - this will surely save you a lot of work.

If you save the configuration in a database, which is a good idea, then you don’t need to mess around with a clunky solution for a shared config file.

The two apps merely shares the same data.

I think I’ll try the Django backend and Yii front end idea… :)

And yes: there’s actually lots of people who use Django for the back-end for existing apps - even php based apps.

Imagine you’re building an e-commerce platform where you develop numerous application components. Some of them belong to the backend, some to the frontend and some are shared between the applications.

If you were to build a shopping cart component, you would only need it in the frontend and then you might have some components you only need in the backend. It would be great if both parts of your platform could be configured together (common config) and separately (frontend and backend configs in respective apps).

Also, most of the models you want to share directly between the apps so generating them with Gii once under common/models saves you a huge amount of time because you only need to maintain a single model instead of two different models, which you would need to if you built the backend with a different framework, e.g. django.

I love Yii because it’s a rapid application development framework which means that I can develop complicated apps quickly compared to most of the other frameworks out there. It would be superb if I could build both my frontend and backend in the same manner taking benefit of all the great features in Yii.

I have quite some experience in creating backend modules and I can tell you that it’s not the way to go if you’re building a large scale application. While it might seems like an easy solution it doesn’t allow you to separate your backend and frontend code-base well enough, at least not in my opinion.

Mike’s idea about having a component which creates another application seems a bit overkill, but it would of course also be an alternative solution.

I’d still like to explore the alternative of creating a custom URL manager to handle the routing. Wouldn’t it be possible to have both shared rules and app-specific rules? What I mean is that we could build an URL manager for which you could configure both shared and app specific URL configurations.

Here’s an example config so that you get a better picture of what I’m talking about:




'urlManager'=>array(

	'class'=>'UrlManager',

	'urlFormat'=>'path',

	'rules'=>array( // shared rules

		......,

	),

	'apps'=>array(

		'frontend'=>array( // frontend config

			'cacheID'=>'dbCache',

			'showScriptName'=>false,

			'urlRuleClass'=>'UrlRule',

			'rules'=>array(

				.....

			),

		),

		'backend'=>array( // backend config

			'cacheID'=>'cache',

			'rules'=>array(

				.....

			),

		),

	),

),



In order words instead of specifying a URL manager component in both applications it could be done in the common config where both applications can be configured both together and separately.

Couldn’t this be working solution or is it too much of a hack as well?

Well, there’s a db url manager somewhere in the extensions area I think… :)

Since the front and the back ends are sharing a database (in my case they do) that’s all that’s needed?

Ah yes I forgot to mention that in most cases my apps are also sharing the same database or databases, otherwise it wouldn’t be possible to manage with a single model for both the frontend and backend.

I took a look at the db url manager and I don’t really see how it’s related to the issue discussed here.

Er… you don’t ?

If the configuration is stored in the database, including the url manager configuration, then that would solve the problem without writing all kinds of meta managers?

Am I missing something? :)

((If I do, then I want to be enlightened…))

So the database would be storing the config for both ends.

Which means that the config needs to carry a parameter to indicate what backend it’s for.

Why go through the trouble of saving the configurations in the database when you could have it in a configuration file instead? I don’t like the idea of having to query the database every time a URL needs to resolved.

Nothing prevents you from caching that information ?

Using the database is much more secure than using the filesystem, across applications.

Sure I could cache it but why query the database in vain? As a developer I would much rather have it in a file so that I don’t need to directly modify the database when I want to change or write functionality in my app that would let me configure the url manager.

As a developer I would rather focus on security and simplicity.

Rather than trying to be clever.

How are you going to deal with allowing only the two involved applications (which, by definition, should be self-contained entities) access to your shared configuration?

Not only when they’re on the same server, but on different servers?

How are you going to handle authentication?

A database already has this built in…

I am just brainstorming here.

You can do this however you want. :)

<edit>

The database solution is tried and tested, debugged and production ready.

</edit>

But, there might be other alternatives out there, if you insist on physical file based solutions.

You could create a third application which sole purpose is controlling access to the shared configuration.

It is a REST server. And if the request has the right API key, it’s granted access.

How about that?

<edit>

It could be Yii, it could be Restler:

http://luracast.com/products/restler/

<edit>

Come to think of it… there’s actually lots of ways you can handle auth: xml-rpc, token-based what-ever-auth, oAuth.

A simple REST-controller with API key is probably enough…

</edit>

Seems a bit overkill to create an application for creating a few URLs from one application to another. Jacmoe, could you explain how you use CHtml::link to link between the applications on the same server?

I just use relative urls to be honest. :)

Like CHtml::link(‘blah’, ‘../../user/login’)

I’m coming late to the party here, so I might have missed something already suggested. But, one solution (without sharing anything) is to simply wrap the createAbsoluteUrl call inside of a controller/action. Each app that you want to participate would get a copy of this controller. Something like:


class UrlController extends CController

{

    public function actionCreateRemote(...)

    {

        // accepts calls from remote app; returns localized absolute url

        echo Yii::app()->createAbsoluteUrl(...);

    }

 

    public static function getRemote($app,...)

    {

        // makes calls to remote app using curl or other method; returns remote absolute url

    }

}

Any apps participating would be listed in each apps config file as an array. Each app would handle creating urls, so that the rules don’t have to be shared. It is still more involved that just creating a link, but not much in the way of coding. On the same server, you could even share the controller code using symbolic links.

Could be like Gii -> allowed IPs.

@bglee: That must be the most elegant solution yet. I really like it, I will have to look into it when I have the time. Thanks for sharing.