Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

offset the URI of an existing Cat App

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


catalyst at iandocherty

Jul 7, 2009, 6:18 AM

Post #1 of 7 (1053 views)
Permalink
offset the URI of an existing Cat App

Hi
I have always written Cat Apps so they start at the '/' URI but now I
have been asked to 'offset' one so that:-

/ becomes /foo
/user becomes /foo/user
/admin/1 becames /foo/admin/1

etc.

I saw the __PACKAGE__->config->{namespace} that could be used but this
would still require an entry in each of my controllers. e.g.

package MyApp::Controller::Root;
...
__PACKAGE__->config->{namespace} = 'foo';
...


package MyApp::Controller::Admin;
...
__PACKAGE__->config->{namespace} = 'foo/admin';
...


etc. which strikes me as very unsatisfactory.

I think I must have missed something. Is there a single point where I
can make a change that will replicate through all my controllers? Can
anyone put me right?

Regards
Ian

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


peter at peknet

Jul 7, 2009, 6:40 AM

Post #2 of 7 (998 views)
Permalink
Re: offset the URI of an existing Cat App [In reply to]

Ian Docherty wrote:

> I think I must have missed something. Is there a single point where I
> can make a change that will replicate through all my controllers? Can
> anyone put me right?

this is done completely at the deployment config level. There's nothing
to modify in your actual code.

How are you deploying the app? FastCGI? mod_perl? etc.

--
Peter Karman . http://peknet.com/ . peter [at] peknet
gpg key: 37D2 DAA6 3A13 D415 4295 3A69 448F E556 374A 34D9

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


orasnita at gmail

Jul 7, 2009, 6:54 AM

Post #3 of 7 (993 views)
Permalink
Re: offset the URI of an existing Cat App [In reply to]

From: "Ian Docherty" <catalyst [at] iandocherty>
> Hi
> I have always written Cat Apps so they start at the '/' URI but now I have
> been asked to 'offset' one so that:-
>
> / becomes /foo
> /user becomes /foo/user
> /admin/1 becames /foo/admin/1

Hi,

If using mod_perl, instead of:

<Location />
SetHandler perl-script
PerlResponseHandler MyApp
</Location>

use:

<Location /foo>
SetHandler perl-script
PerlResponseHandler MyApp
</Location>

And the application should work at the new location.

But if in the templates you use urls like:

<a href="/user">...</a>

you need to change them to urls like:
<a href="[% c.uri_for('/user') %]">...</a>

If you use fastcgi, instead of:

FastCgiExternalServer /tmp/myapp.fcgi -socket /tmp/myapp.socket
Alias / /tmp/myapp.fcgi/

you could use:

FastCgiExternalServer /tmp/myapp.fcgi -socket /tmp/myapp.socket
Alias /foo /tmp/myapp.fcgi/

Note! I know these only from theory, because I always needed to use apps
only at "/".

HTH.

Octavian



>
> etc.
>
> I saw the __PACKAGE__->config->{namespace} that could be used but this
> would still require an entry in each of my controllers. e.g.
>
> package MyApp::Controller::Root;
> ...
> __PACKAGE__->config->{namespace} = 'foo';
> ...
>
>
> package MyApp::Controller::Admin;
> ...
> __PACKAGE__->config->{namespace} = 'foo/admin';
> ...
>
>
> etc. which strikes me as very unsatisfactory.
>
> I think I must have missed something. Is there a single point where I can
> make a change that will replicate through all my controllers? Can anyone
> put me right?
>
> Regards
> Ian
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


jspath at pangeamedia

Jul 7, 2009, 7:01 AM

Post #4 of 7 (996 views)
Permalink
Re: offset the URI of an existing Cat App [In reply to]

Octavian Râsnita wrote:
> But if in the templates you use urls like:
>
> <a href="/user">...</a>
>
> you need to change them to urls like:
> <a href="[% c.uri_for('/user') %]">...</a>

There is an alternative to using uri_for in every link:

<base href="[% c.request.base %]">

...

<a href="user"></a>

This method will work whether your deployment is at the base of the
domain, or a subdirectory.

We used this method so that we could run applications under user
directories:

http://dev.server.com/~someuser/app1/

But then on production have them run at the base of their domain.

http://app1.domain.com/

- Jim

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


orasnita at gmail

Jul 7, 2009, 7:39 AM

Post #5 of 7 (995 views)
Permalink
Re: offset the URI of an existing Cat App [In reply to]

From: "Jim Spath" <jspath [at] pangeamedia>
Octavian Râsnita wrote:
> But if in the templates you use urls like:
>
> <a href="/user">...</a>
>
> you need to change them to urls like:
> <a href="[% c.uri_for('/user') %]">...</a>

There is an alternative to using uri_for in every link:

<base href="[% c.request.base %]">

...

<a href="user"></a>

This method will work whether your deployment is at the base of the
domain, or a subdirectory.

Thanks for remembering about <base>. I think that this technique won't work
if the links start with "/" though, like:

<a href="/user"></a>

So, it is better to create the links using uri_for() from the beginning or
use only relative links...

Octavian




_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


catalyst at iandocherty

Jul 7, 2009, 8:17 AM

Post #6 of 7 (982 views)
Permalink
Re: offset the URI of an existing Cat App [In reply to]

Octavian Râsnita wrote:
> From: "Ian Docherty" <catalyst [at] iandocherty>
>> Hi
>> I have always written Cat Apps so they start at the '/' URI but now I
>> have been asked to 'offset' one so that:-
>>
>> / becomes /foo
>> /user becomes /foo/user
>> /admin/1 becames /foo/admin/1
>
> Hi,
>
> If using mod_perl, instead of:
>
> <Location />
> SetHandler perl-script
> PerlResponseHandler MyApp
> </Location>
>
> use:
>
> <Location /foo>
> SetHandler perl-script
> PerlResponseHandler MyApp
> </Location>
>
> And the application should work at the new location.
>
Thanks, that did the trick. I was looking for something more complicated!

> But if in the templates you use urls like:
>
> <a href="/user">...</a>
>
> you need to change them to urls like:
> <a href="[% c.uri_for('/user') %]">...</a>
>
> If you use fastcgi, instead of:
>
> FastCgiExternalServer /tmp/myapp.fcgi -socket /tmp/myapp.socket
> Alias / /tmp/myapp.fcgi/
>
> you could use:
>
> FastCgiExternalServer /tmp/myapp.fcgi -socket /tmp/myapp.socket
> Alias /foo /tmp/myapp.fcgi/
>
> Note! I know these only from theory, because I always needed to use apps
> only at "/".
>
> HTH.
>
> Octavian
>
>
>
>>
>> etc.
>>
>> I saw the __PACKAGE__->config->{namespace} that could be used but this
>> would still require an entry in each of my controllers. e.g.
>>
>> package MyApp::Controller::Root;
>> ...
>> __PACKAGE__->config->{namespace} = 'foo';
>> ...
>>
>>
>> package MyApp::Controller::Admin;
>> ...
>> __PACKAGE__->config->{namespace} = 'foo/admin';
>> ...
>>
>>
>> etc. which strikes me as very unsatisfactory.
>>
>> I think I must have missed something. Is there a single point where I
>> can make a change that will replicate through all my controllers? Can
>> anyone put me right?
>>
>> Regards
>> Ian
>>
>> _______________________________________________
>> List: Catalyst [at] lists
>> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>> Searchable archive:
>> http://www.mail-archive.com/catalyst [at] lists/
>> Dev site: http://dev.catalyst.perl.org/
>
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/
>
>


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


larryl at emailplus

Jul 7, 2009, 8:23 AM

Post #7 of 7 (992 views)
Permalink
Re: offset the URI of an existing Cat App [In reply to]

Hi Ian -

> I have always written Cat Apps so they start at the '/' URI but now I
> have been asked to 'offset' one so that:-
>
> / becomes /foo
> /user becomes /foo/user
> /admin/1 becames /foo/admin/1

One approach is to modify $c->prepare_path, similar to:

http://dev.catalyst.perl.org/wiki/wikicookbook/urlpathprefixing

(The example was written for Catalyst 5.7 and will work with 5.8, but
there's probably a Moose-ier way to do the same in 5.8...)

Something like:

------

package MyApp;

use Catalyst::Runtime '5.70';
use base 'Catalyst';

__PACKAGE__->setup();

sub prepare_path
{
my $c = shift;
$c->NEXT::prepare_path(@_);

my @path_chunks = split m[/], $c->request->path, -1;

# Pull prefix off beginning of request path:
my $prefix = shift @path_chunks;

# Create modified request path from any remaining path chunks:
my $new_path = join('/', @path_chunks) || '/';
$c->request->path($new_path);

# Modify the path part of the request base
# to include the path prefix:
my $base = $c->request->base;
$base->path($base->path . "$prefix/");

return;
}

------

So if the url was "/foo/admin/1", the controllers will see a request for
"/admin/1". Also, $c->uri_for('some/path') will generate
"/foo/some/path".



HTH,
Larry

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.