+ Reply to Thread
Results 1 to 9 of 9

Thread: Use only one controller and view script?

  1. #1
    BJM
    BJM is offline Junior Member
    Join Date
    Apr 2008
    Posts
    8

    Default Use only one controller and view script?

    Hi all,

    I've read some stuff on modules however I'm new to Zend Framework and MVC, and just don't seem to be understanding it with regards to normal websites. Is there a way to have a default controller that handles all http requests?

    I'm creating a very simply CMS that allows a user to add/edit/delete pages (and child pages). I want a module setup to handle the admin area (mysite.com/admin), and within this admin area I want a full application for adding content. This seems to be the easy part. The part I don't understand is how to make every other type of request (i.e. mysite.com/abc, mysite.com/123, mysite.com/abc/def/ghi, etc) use a default controller and view script. Other than the admin area, every page should work exactly the same; the only difference being the file it pulls content from, which will depend on the URL being requested. Advice would be great.

  2. #2
    Toppy is offline Junior Member
    Join Date
    Mar 2008
    Location
    BC
    Posts
    6

    Default

    I could be wrong but what it sounds like is you want a standard 'template' for every page?

    If so, I would suggest checking out the Zend Framework Webinars

    In particular there is one called 'Zend_Layout and Zend_View Enhancements'. This will show you how to use the new ZF 1.5 Layouts to have a standard layout with view content being inserted in it.

    Also, check out: PHP VIDEO TUTORIALS FOR WEB DESIGNERS. There are some good screencasts by Jon Lebensold. I believe one touches on the new Layout features. It's a good series to go through.

  3. #3
    BJM
    BJM is offline Junior Member
    Join Date
    Apr 2008
    Posts
    8

    Default

    Thank you for the reply Toppy, however I believe you misunderstood. I actually have viewed the new layout webinar and do plan on using that approach, however my question was related to the framework files (mostly controllers), not just the layout. For instance, take the following URLs:

    mysite.com/index
    mysite.com/about_us
    mysite.com/services/
    mysite.com/services/service1
    mysite.com/services/service1/features
    mysite.com/services/service2
    mysite.com/services/service1/pricing

    My understanding of Zend Framework and MVC is that I'd need a controller for index, about_us, and services. In addition, I'd need view scripts for service1 and service2. Anything after the view sript is supposed to be variable/value pairs, so I don't even know how that would work (third, fourth, fifth level directories), but it's besides the point. The problem is that with a CMS I have no idea what pages the user will create, and how many child pages each page will have. Let's say a user wants to create two new pages, one being mysite.com/contact_us, and the other being mysite.com/services/service3. From a coding standpoint, do I need to dynamically create a new controller and view script for the contact_us page, and dynamically create a new view script in the already created services view script area for service3? I plan on storing the actual page content in a database or XML file, therefore the view scripts won't even contain the main content, so it would be nice if I can have one controller and view script handle everything.

    BJM

  4. #4
    Ewok is offline Junior Member
    Join Date
    Mar 2008
    Location
    Colorado SPrings
    Posts
    16

    Default

    Most systems use something like this...
    mysite.com/cmscontroller/id/1234

    So the index view of cmscontroller displays the DB entry with the id of 1234

  5. #5
    BJM
    BJM is offline Junior Member
    Join Date
    Apr 2008
    Posts
    8

    Default

    hmmm .. Thank you for the feedback, unfortunately I was hoping for a much slicker method. Using parameter/value pairs (mysite.com/cmscontroller/id/1234) is good for viewing things like news items or article items, however I don't like that approach for actual page names. In my experience clients can be picky about their URLs, especially when they advertise them. I think people may complain if I tell them to print "mysite.com/services/id/1234" on a brochure instead of "mysite.com/services/service1/features/".

    In addition, this wouldn't resolve the problem of a site with 20 or 50 (although that may be unrealistic) "top level" pages (mysite.com/a, mysite.com/b, mysite.com/c, mysite.com/d, etc). It seems odd that I would be forced to create a different controller for all of these pages even though the logic for them will be the exact same. Either I'm misunderstanding (which I admit is probably the case), or MVC is better left to web applications, not basic websites with tonnes of pages. Separating content from process is great, but now I'm stuck with choosing between unfriendly URL's, or having redundant controller files that all do the same thing.

  6. #6
    Leif.Högberg is offline Member
    Join Date
    Aug 2007
    Location
    Sweden
    Posts
    52

    Default

    Or you could use a custom route. Look into Zend_Controller_Router_* and I'm sure you will find what you need.

  7. #7
    BJM
    BJM is offline Junior Member
    Join Date
    Apr 2008
    Posts
    8

    Default

    Thank you Leif.Högberg. This may be exactly what I'm looking for. I'll look into it further.

  8. #8
    BJM
    BJM is offline Junior Member
    Join Date
    Apr 2008
    Posts
    8

    Default

    If anyone is interested, I've been able to solve the issue using routes:

    [php]
    $router = $front->getRouter();
    $router->addRoute(
    'default',
    new Zend_Controller_Router_Route('*', array(
    'controller' => 'index',
    'action' => 'index')));

    $router = $front->getRouter();
    $router->addRoute(
    'admin',
    new Zend_Controller_Router_Route('admin/:controller/:action/*', array(
    'module' => 'administration',
    'controller' => 'index',
    'action' => 'index')));
    [/php]Not sure if this is the best approach since I'm new to Zend Framework ... but it works . Thanks for the replies.

  9. #9
    Phunky is offline Junior Member
    Join Date
    Apr 2008
    Posts
    6

    Default

    I'm looking at doing a similar thing and was thinking of setting up a StaticController which would hold all the static pages within /app/views/scripts/static/.

    I was then going to setup static routes for each one, you how ever have done it for all URLs how did you go about flagging Error pages?!

+ Reply to Thread

Similar Threads

  1. Using a different script in a view
    By orlox in forum Model-View-Controller (MVC)
    Replies: 3
    Last Post: 08-25-2009, 07:42 PM
  2. View script directory name
    By ivan.skugor in forum Model-View-Controller (MVC)
    Replies: 2
    Last Post: 05-31-2009, 12:24 PM
  3. Rendering view script of another controller?
    By conradwt in forum Model-View-Controller (MVC)
    Replies: 4
    Last Post: 08-15-2008, 12:54 PM
  4. How to implement an XML view script?
    By conradwt in forum Model-View-Controller (MVC)
    Replies: 0
    Last Post: 04-03-2008, 10:27 AM
  5. view script name in view helper
    By rainco in forum Model-View-Controller (MVC)
    Replies: 0
    Last Post: 09-16-2007, 10:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts