SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Enthusiast <?php's Avatar
    Join Date
    Mar 2006
    Location
    Warwickshire, UK
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Zend Framework Modules

    Hi all,

    I have now been searching the internet high and low for a few weeks in hope of finding the answer I require to my Smarty / Modules problem with Zend. As always if you come to SitePoint you get help unlike others... Please bare with me on this one...

    Basically, I am trying to create a module called courses so the end url would be
    Code:
    http://domain.com/courses/page-url
    which I think is a pretty simple task.
    My application Structure is as follows:
    Code:
    application
       Configuration
       Controllers
       Courses
          Controllers
             IndexController.php
             CourseController.php
       Smarty
       Models
       Zend
    Then the in the BootStrap (index.php) in public_html I have

    Code:
    $controller = Zend_Controller_Front::getInstance();
    $controller->setControllerDirectory(array(
    	'default'   =>  $config->paths->base . 'include/Controllers',
    	'courses'   =>  $config->paths->base . 'include/Courses/Controllers'
    	));
    
    $controller->registerPlugin(new CustomControllerAclManager($auth));
    
    $controller->throwExceptions(true);
    	
    $vr = new Zend_Controller_Action_Helper_ViewRenderer();
    $vr->setView(new Templater());
    $vr->setViewSuffix('tpl');
    Zend_Controller_Action_HelperBroker::addHelper($vr);
    	
    $router = $controller->getRouter();
    $router->addConfig(new Zend_Config_Ini('../include/Configuration/routes.ini', 'routes'), 'routes');
     
    $controller->dispatch();
    Problem: When I goto http://domain.com/courses it seems to be showing the main front page from my templates folder (/public_html/templates/index/index.tpl)

    I have spoken to Mr. Zervass from PHPRiot who's book I'm reading & he told me to add a route:

    routes.course.route = "courses/:url/:action/*"
    routes.course.defaults.module = courses
    routes.course.defaults.controller = course
    routes.course.defaults.action = index

    Example uses modules as well as controllers and action (effectively adding an extra level of grouping the actions)

    This basically means if the "action" parameter is blank then "index" is used.

    This class is then declared as:
    class Courses_CourseController extends CustomControllerAction
    I have tried and tried on this for around 2 weeks now everynight and still can't seem to get it to work -

    I can understand-ish why it's showing the index/index template and not courses/index.tpl template but nothing I try seems to fix it.

    Is it possible to use CourseController.php as the default in the courses folder instead of IndexController.php ref: "routes.course.defaults.controller = course" ? confused

    Hoping someone can shed light on this for me....

    Kind regards
    Nick T

  2. #2
    SitePoint Addict
    Join Date
    May 2003
    Location
    Auckland
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I think the problem is http://domain.com/courses isn't the right URL to match what you've done.

    I think it goes something like this:

    http://domain.com/courses checks for a module named "courses" - which you've got. Then, since there's no Controller/Action pair after the module name it'll automatically set Controller/Action to index/index.

    So http://domain.com/courses should try and load "indexAction" from /application/Courses/Controllers/IndexController.php

    Try calling http://domain.com/courses/index/index

    Does this resolve to the same page?

    The actual URL to get to the indexAction of /application/Controllers/Courses/CourseController.php is
    http://domain.com/courses/courses/index/

    So, why do you need a separate module for courses when you could just have a standard Controller. E.g.

    PHP Code:
    <?php

    require_once "Zend/Controller/Action.php";

    class 
    CoursesController extends Zend_Controller_Action
    {
        public function 
    pageUrlAction()
        {
            echo 
    'Hello!';
        }
    }
    The URL to this action above is http://domain.com/courses/page-url/ which is what you want?

  3. #3
    SitePoint Enthusiast <?php's Avatar
    Join Date
    Mar 2006
    Location
    Warwickshire, UK
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Imminent,

    Thank you for getting back to me, appreciated.

    If I goto courses/index/index it still resolves to the same page

    If I goto courses/courses/index then it seems to work... Is it possible to make it just Courses/index with a module?

    To be honest I have been thinking alot over the weeks as to go for a standard controller or module...?

    I wanted to have all sections on the site (not only courses) so they are similar to:

    domain.com/courses/url

    which so far I have all working from the database etc but wanted to have the url part as a different template and not use the index of the controller. I didn't want to another action such as viewAction() to make my urls like

    domain.com/courses for the listings
    domain.com/courses/view/url for the individual courses

    Not sure if im making sense

    This is what I have been building and this page is pretty much what im talking about: http://www.digitalmediastudio.co.uk/...pment-courses/

    At the minute if you click a listing as it is it loads the content on the top of that page along with listings below it. What are your thoughts ???

    Hope you understand and can help me

    Kind regards

    Nick

  4. #4
    SitePoint Addict
    Join Date
    May 2003
    Location
    Auckland
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What you should do is rename Courses_CoursesController to Courses_IndexController. There's no point "namespacing" the Courses controller a second time. Here's what Courses_IndexController should look like:

    PHP Code:
    <?php

    require_once "Zend/Controller/Action.php";

    class 
    Courses_IndexController extends Zend_Controller_Action
    {
        public function 
    indexAction()
        {
            echo 
    'I should be the URL: /courses/ or /courses/index/';
        }
        
        public function 
    viewAction()
        {
            
    $url $this->_request->getParam('url');
        
            echo 
    'I should be the URL: /courses/index/view/ (url: ' $url ')';
        }
    }
    Once that works you can use a route so that you can make /courses/view/url work, as well as /courses/index/view/url.

    So add this route:
    Code:
    routes.course.route           = "courses/view/:url/*"
    routes.course.defaults.module         = courses # courses module
    routes.course.defaults.controller      = index # the controller is now index
    routes.course.defaults.action          = view # use the view action
    PHP Code:
    <?php

    require_once "Zend/Controller/Action.php";

    class 
    Courses_IndexController extends Zend_Controller_Action
    {
        public function 
    indexAction()
        {
            echo 
    'I should be the URL: /courses/ or /courses/index/';
        }
        
        public function 
    viewAction()
        {
            
    $url $this->_request->getParam('url');
        
            echo 
    'I should be the URL: /courses/index/view/ (url: ' $url ')';
            echo 
    'I might also be the URL: /courses/view/' $url '/');
        }
    }
    I think that should work.

  5. #5
    SitePoint Enthusiast <?php's Avatar
    Join Date
    Mar 2006
    Location
    Warwickshire, UK
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Imminent,

    I have followed your advise but I still seem to have the same issue. When I go to domain.com/courses it's rendering the default index/index.tpl template instead of asking for courses/index.tpl in which I want.

    Is it using this template as it's using IndexController although in a module? Is it possible to have the default controller in that module something like CoursesController.php instead of IndexController.php?

    Refering back to your post:
    I would like to have all my pages in the style of

    Code:
    domain.com/events/
    domain.com/events/event-name
    What is the best way to link to individual events from the main events? So if no event can be found should redirect back to events main page. I nearly have this working now on a standard controller as you can see.

    What I'm asking overall is without me using:
    Code:
    domain.com/events
    domain.com/events/view/event
    How can I just have:
    Code:
    domain.com
    domain.com/events/event
    but use a different template for the individual event and use the index template for the main events page. How do I assign a template to whatever the url (event) will be seperate without having to use an action etc?

    I think im very confused to say the least with modules & routes etc - I don't find the official doc on it all that brilliant..

    Cheers
    Nick

  6. #6
    SitePoint Addict
    Join Date
    May 2003
    Location
    Auckland
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I've attached a project that's setup to do what you want.

    It has an IndexController for the homepage.
    And EventsController for listing and viewing events.

    Events can be accessed using the following url:

    /events/event-one/
    /events/event-two/

    I've tried to document what I've done in each file.
    Attached Files Attached Files

  7. #7
    SitePoint Enthusiast <?php's Avatar
    Join Date
    Mar 2006
    Location
    Warwickshire, UK
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Imminent

    Ihii yeah it maybe best - thanks very much again!

    Will take a look

    Nick

  8. #8
    <?php while(!sleep()){code();} G.Schuster's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like you don't understand the module concept of ZF.

    Example WITH modules:
    / - module "default", controller "default", action "default"
    /courses/ - module "controller", controller "default", action "default"
    /courses/course/ - module "controller", controller "course", action "default"
    /courses/course/view/ - module "controller", controller "course", action "view"

    Example WITHOUT modules:
    / - module N/A, controller "default", action "default"
    /courses/ - module N/A, controller "courses", action "default"
    /courses/view/ - module N/A, controller "courses", action "view"

    So, if you call /courses/ WITH modules it's just correct that ZF calls your index/index.tpl - as you DO HAVE the default controller "index" and default action "index" by not specifiying them explicitly.
    Skip the modules - I think the most things can be done within controllers.
    Personally, I use modules only for separation of different frontends, e.g. a public "frontend" and an administrative "management" module.

  9. #9
    SitePoint Enthusiast <?php's Avatar
    Join Date
    Mar 2006
    Location
    Warwickshire, UK
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Imminent & G.Schuster,

    Thank you to you both for your kind help. I have gone through both of what you have sent & said and it all makes sense now.

    Thank you both again

    Kind regards

    Nick


Bookmarks

Posting Permissions

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