Skip to content

Multiple handles for bundles #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

mileswebdesign
Copy link

Hi Taylor,

I made a change to the bundle class, which allows one to set multiple handles for a bundle. It's a simple change, that won't add a lot of extra overhead, but makes the routing of bundles a bit more flexible.

A practical example would be when you have two separate blogs on your website (for example in different languages). They would both use the same blog bundle, but serve up different content based on the URI. With this addition you don't have to include a reference to 'blog' anymore like /blog/myfirstblog and /blog/mysecondblog. Instead you can simply configure multiple handles and use /myfirstblog and /mysecondblog directly. Looks a bit cleaner, I think.

Hope you like it :)

… in the bundle configuration

return array(

	'blog' => array('handles' => array('blog', 'my_blog') ),
);

The blog-bundle will now be revoked for mydomain.com/blog as well as mydomain.com/my_blog
The original configuration with a single string, can still be used as well
@ProgerXP
Copy link

ProgerXP commented May 5, 2012

Hmm, this looks nice. You could however shorten your code by eliminating the 'else' branch like this:

foreach (static::$bundles as $key => $value)
{
  if (isset($value['handles']))
  { 
    // Let's see if one of them matches the uri
    foreach ((array) $value['handles'] as $handles)
    {
      if (starts_with($uri, $handles.'/'))
      {
        // replace the array with a string with the matching handles and return the key
        static::$bundles[$key]['handles'] = $handles;
        return $key;
      }
    }
  }
}

@gcaplan
Copy link

gcaplan commented May 10, 2012

+1 for adding this to the core, please.

Use case: system for making multiple storefronts that share a lot of code, but have some site-specific code. One approach would be to put each storefront into a bundle, but then it would have to handle multiple URI roots, such as mystore/products, mystore/basket, mystore/checkout etc.

@franzliedke
Copy link
Contributor

👍

@bencorlett
Copy link
Contributor

+1

@daylerees
Copy link
Contributor

What happens when you use (:bundle). In a route with this? I thought that came from the 'handles' param.

@mileswebdesign
Copy link
Author

@daylerees: I had looked into this when I coded the new feature. So far I haven't had situations where routes with (:bundle) weren't set properly. I've been using this code for about 3 weeks now and haven't experience any errors with it. Also with v3.2.

That off course doesn't mean that the code is bullet-proof. Perhaps it's a good idea if a bunch of people try out the code in their project, to see if it breaks anything.

@franzliedke
Copy link
Contributor

This is huge! Please merge it :)

@robotamer
Copy link

good one

@tkaw220
Copy link
Contributor

tkaw220 commented Jun 29, 2012

+1

@sparksp
Copy link
Contributor

sparksp commented Jul 19, 2012

@mileswebdesign wrote:

That off course doesn't mean that the code is bullet-proof. Perhaps it's a good idea if a bunch of people try out the code in their project, to see if it breaks anything.

Has anyone else had a chance to play with this, especially using (:bundle) to see what impact it has and whether anything breaks? Additionally, does this need any documentation changes to support it?

@yuters
Copy link
Contributor

yuters commented Jul 21, 2012

Seems to break in the Router class. Can we add something like this ?

foreach ((array) static::$bundle as $handle)
{
    $uri = str_replace('(:bundle)', $handle, $uri);
}

@mileswebdesign
Copy link
Author

@yuters Can you post your code (the actual route) that breaks the router class in your setup? That way we have something to test with.

@yuters
Copy link
Contributor

yuters commented Jul 21, 2012

Of course, but I'm really just starting to work with this framework. If I create a simple admin bundle, adding this to application/bundles.php :

'admin' => array(
    'handles' => array('admin', 'cms'),
),

and in my admin bundle, I have a simple routes.php

Route::get('(:bundle)', function()
{
    return 'hello Word!';
});

everything works fine.

But if I add 'auto' => true in the application/bundles.php for the admin, I get this error (array to string conversion) in the Router class. But maybe I'm doing something wrong here?

The fix I tried to make returns a 404 for cms/

@yuters
Copy link
Contributor

yuters commented Jul 21, 2012

How about this?

foreach ((array) static::$bundle as $handles)
{
    if (starts_with(\URI::current(), $handles))
    {
        $uri = str_replace('(:bundle)', $handles, $uri);
    }
}

@yuters
Copy link
Contributor

yuters commented Jul 21, 2012

Although it works for me now... maybe using \URI::current() isn't such a good idea. Maybe there's a way to call the Router register method again for each handles?

@sparksp
Copy link
Contributor

sparksp commented Jul 31, 2012

@mileswebdesign it looks like there's still an unresolved issue using "(:bundle)" in a route when the bundle is auto loaded?

@taylorotwell
Copy link
Member

Where are we at on this? Still outstanding issues?

@mileswebdesign
Copy link
Author

Hi guys, sorry I just returned from holidays and haven't had a chance to look into this request further. I have an idea of how to solve the issue raised and hope to come up with a solution by this weekend.

@daylerees
Copy link
Contributor

Any progress with this one? Thanks!

@taylorotwell
Copy link
Member

Will address this issue in next major release. Focusing mainly on bug fixes for Laravel 3.x at this point.

@TedAvery
Copy link

TedAvery commented Oct 4, 2012

+1

@swt83
Copy link

swt83 commented Dec 16, 2012

I found a case where I need this ability also. So +1 from me too.

@tester5
Copy link

tester5 commented Jan 16, 2013

OK, we have a need to this too however not all routes work when I make this change after applying this patch:

When I change my bundles.php entry from:

'atable' => array('handles' => 'contacts')),

to

'atable' => array('handles' => array('contacts','messages')),

I receive the following error on some of my routes (some routes work and some don't!):

Unhandled Exception

Message:

Array to string conversion
Location:

/home2/xxx/xx/laravel/routing/router.php on line 209
Stack Trace:

#0 /home2/xxx/xx/laravel/laravel.php(40): Laravel\Error::native(8, 'Array to string...', '/home2/xxx...', 209)
#1 [internal function]: Laravel{closure}(8, 'Array to string...', '/home2/xxx...', 209, Array)
#2 /home2/xxx/xx/laravel/routing/router.php(209): str_replace('(:bundle)', Array, '(:bundle)')
#3 /home2/xxx/xx/laravel/routing/route.php(318): Laravel\Routing\Router::register('POST', '(:bundle)', Array)
#4 /home2/xxx/xx/bundles/atable/routes.php(7): Laravel\Routing\Route::post('(:bundle)', Array)
#5 [internal function]: Laravel\Bundle::{closure}()
#6 /home2/xxx/xx/laravel/routing/router.php(151): call_user_func(Object(Closure))
#7 /home2/xxx/xx/laravel/routing/route.php(366): Laravel\Routing\Router::group(Array, Object(Closure))
#8 /home2/xxx/xx/bundles/atable/routes.php(75): Laravel\Routing\Route::group(Array, Object(Closure))
#9 /home2/xxx/xx/laravel/bundle.php(136): require('/home2/xxx...')
#10 /home2/xxx/xx/laravel/routing/router.php(397): Laravel\Bundle::routes('atable')
#11 /home2/xxx/xx/laravel/url.php(282): Laravel\Routing\Router::find('import')
#12 /home2/xxx/xx/laravel/view.php(366) : eval()'d code(17): Laravel\URL::to_route('import')
#13 /home2/xxx/xx/laravel/view.php(366): eval()
#14 /home2/xxx/xx/laravel/blade.php(71): Laravel\View->get()
#15 [internal function]: Laravel\Blade::Laravel{closure}(Object(Laravel\View))
#16 /home2/xxx/xx/laravel/event.php(199): call_user_func_array(Object(Closure), Array)
#17 /home2/xxx/xx/laravel/event.php(138): Laravel\Event::fire('laravel.view.en...', Array, true)
#18 /home2/xxx/xx/laravel/view.php(337): Laravel\Event::until('laravel.view.en...', Array)
#19 /home2/xxx/xx/laravel/view.php(570): Laravel\View->render()
#20 /home2/xxx/xx/laravel/response.php(246): Laravel\View->__toString()
#21 /home2/xxx/xx/laravel/laravel.php(178): Laravel\Response->render()
#22 /home2/xxx/public_html/index.php(34): require('/home2/xxx...')
#23 {main}

So should I raise this as a separate issue?

@sparksp
Copy link
Contributor

sparksp commented Jan 16, 2013

@tester5 please do not raise a separate issue, it's a problem with this patch and not how laravel works at present.

@tester5
Copy link

tester5 commented Jan 16, 2013

ok no worries, will track this pull request. This issue only presents itself when I use the array notation when trying to register more than one handle. and strangely, some routes still work and now some don't :-0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.