Domanda

I have two separate Laravel instances/sites running on a server and want to be able to generate a url to a named route on one from code within the other.

For example the following named route exists in the first instance: Route::get('users/my_account', array('uses' => 'UsersController@myAccount', 'as' => 'my_account'))

In the second instance I want to generate a url to the route above. Can anyone think of a clean way to do this, without explicitly knowing the url (i.e. only knowing the name of the route 'my_account')?

Basically I want to expose the RouteCollection of one site to the other...

È stato utile?

Soluzione

That's a pretty interesting question. There's no natively supported way of doing that and, from what I know, it won't ever.

You could try loading the routes file of the first application, parsing it's configurations (you will need that for reverse routing), construct a Router instance and use it, but I'm sure it won't be simple at all.

If you have a really, I mean really, good reason to use reverse routes, you can try building a small API on the first application. That API should receive the parameters necessary, those used in url($params), and return the full url (with domain and everything). Although, this will introduce some serious performance issues.

IMHO, stick to hard coded my_account, leave a comment on the first application route and/or controller explaining that it's used on another project and move on :)

Altri suggerimenti

I may call this a dirty trick. Supposing you have the following structure in your file system, where both paths are Laravel apps:

/path/to/apps/app1
/path/to/apps/app2

And you want to load the routes file from app1 into app2. You can do it as follow:

include "../app1/apps/routes.php";
$url = URL::route('register');

Voilà! But, although it worked for me there are some points to consider.

  • Include that file, will surely overwrites your current route collection for that process.
  • If that last is true, then you can have problem generating other URLs, maybe in your views.
  • The domain name will be of the current Laravel instance. It means that your URLs generated into app2 will hold the domain name of app1. I believe this is not what you want. But you can always generate non-absolute URLs with URL::route('register', null, false).
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top