Catalyst Controller Questions

2019-05-16 12:58发布

问题:

I just used catalyst for my first large project and I was left wondering if I used catalyst in the way it was meant to be used.

I have Root.pm and inside of that file I put multiple url handlers.

Is it a best practice to have one controller per url or should a grouping be considered?

回答1:

One of the beauties of Catalyst is its flexibility. You can do this however it best suits your application.

If you only have a handful of URLs you support, then there's nothing intrinsically wrong with putting them all in Root.pm. Particularly if there's no depth, ie localhost:3000/foo and localhost:3000/bar

However, as soon as you start to have longer URLs such as localhost:3000/foo/bar/baz/quux where baz and quux are arguments to bar, you'll see the benefit of separating out a Foo.pm that contains an action (sub) called 'bar'. (And that's before we get into the joys of Chained Actions...)

Although there are ways that you can achieve the equivalent of a Rails style routing table, it's not generally considered to be a good idea. Not having a routes table is an intrinsic feature/benefit of Catalyst over other frameworks.

There's a good discussion of this on pages 13-14 of The Definitive Guide to Catalyst. If you don't have this book, you should.