angular-route-segment icon indicating copy to clipboard operation
angular-route-segment copied to clipboard

Added support of redirectTo instruction at segment params.

Open wingedfox opened this issue 12 years ago • 8 comments

Useful for managing external and internal redirects within a route definitions

Syntax:

$routeSegmentProvider
    .when('/section', 's1')
    .when('/section/index', 's1.idx')
    .segment('section', {
         redirectTo: 'section/index'
     })
    .within()
        .segment('idx', {
             templateUrl: '/views/segment/idx.html'
         });

wingedfox avatar Oct 04 '13 12:10 wingedfox

Great work! Right now I'm creating a separate controller for every route I want to redirect to. It'd be really nice to see this merged. But I think you might have to add tests for this to happen...

chmanie avatar Dec 17 '13 14:12 chmanie

@wingedfox Your provided example could be easily rewrited like this:

$routeSegmentProvider
    .when('/section', 's1.idx')  // you can use deep-level segment here
    .when('/section/index', 's1.idx')
    .segment('section', {
         template: '<div app-view-segment="1"></div>'
     })
    .within()
        .segment('idx', {
             templateUrl: '/views/segment/idx.html'
         });

There is no need in separate redirectTo functionality for such a case. Also, you can use $routeProvider's redirecting as well:

$routeProvider.when('/invalidUrl', {redirectTo: '/validUrl'});
$routeSegmentProvider.when('/validUrl', 'segment')
.segment('segment', ...);

But your latter commits look really interesting. Could you please provide tests on it?

artch avatar Dec 18 '13 20:12 artch

@artch, even if I can buy a piglet, I prefer not to do that :) In your example you'll have all the relative links broken. Redirects in my example are used for hiding gaps in the navigation tree, I prefer to cover them by local redirects instead of a global ones. E.g. at a given time I have a mapping

/    -> /a
/a   -> /a/b
/a/b -> /a/b/c

for sure this does not mean that the following will be correct at any given moment

/  -> /a/b/c
/a -> /a/b/c

when a root page is added inner redirects will remain intact.

I'll try to add the tests, but I don't work on that project anymore so first I'll have to remember what's going on there. Anyway, it will take a place not before the mid-january, because I'll be on a vacation.

wingedfox avatar Dec 19 '13 07:12 wingedfox

A bit unclear for me. You wrote:

$routeSegmentProvider
    .when('/section', 's1')
    .when('/section/index', 's1.idx')
    .segment('section', {
         redirectTo: 'section/index'
     })

This piece of configuration is identical to:

$routeSegmentProvider
    .when('/section', 's1.idx')  
    .when('/section/index', 's1.idx')

Isn't it?

artch avatar Dec 19 '13 07:12 artch

Following your way link from template

<a href="b" />

with /section in the address bar it will point to /b with /section/index in the address bar it will point to /section/b

When using redirects, you'll never see /section in the address bar, you always will be redirected to /section/index.

wingedfox avatar Dec 19 '13 08:12 wingedfox

OK, why not to use built-in angular $routeProvider.when('/section', {redirectTo: '/section/index'}) for this case? I really see no reason for implementing redirects at segments level of abstraction.

artch avatar Dec 19 '13 08:12 artch

For me, this is the vital case. Without this feature I'll have the dozens of

$routeProvider.when('/section', {redirectTo: '/section/index'})

instructions, placed somewhere out of context, where the section is defined. Take a look:

$routeProvider.when('/', {redirectTo: '/reports/market/au/slices'})
$routeProvider.when('/reports', {redirectTo: '/reports/market/au/slices'})
$routeProvider.when('/reports/market', {redirectTo: '/reports/market/au/slices'})
$routeProvider.when('/reports/market/au', {redirectTo: '/reports/market/au/slices'})
$routeProvider.when('/reports/market/value', {redirectTo: '/reports/market/value/sum'})

Here I have to change 3 lines to make /reports/market/value/sum default report. With per-segment redirects I'll have to change only one line. I have a script for building the routing table for the reports and I'll prefer to generate as few lines as possible.

wingedfox avatar Dec 19 '13 08:12 wingedfox

I see. How do you assign segments to URLs in the case of these 5 routes?

artch avatar Dec 19 '13 16:12 artch