• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with proper URL/URI setup

#1
[eluser]rt30000[/eluser]
I've spent some time trying to get this project halfway working to show my issues better than I can translate:

Here is the test URL: CI Project Link

As you can see, I just cannot figure out URLs or URIs. I have to use "index.php/page/index/4" (where 4 is the $id of the page to display) rather than a more friendly url. You can click through the pages, all the top level ones are there. The home page, because it has no $id (just goes straight to default controller "page") throws two errors. Ideally, I would like more clear urls and remove the index.php. I tried using .htaccess but I think my server is not accepting it. Is that the first step I need to solve?

I really appreciate any help you can lend me...this is my first stab at MVC. Everything on those pages is based off a single template (with sub views), a model and the Page controller. I think i'm beginning to understand the basic concept.

Here is my controller just in case it helps.

Code:
<?php

class Page extends Controller {
    
    function Page()
    {
        parent::Controller();
        $this->load->model('page_model');
    }
    
    function index($id)
    {
        /* Determine which page this is by using supplied id... */
        if (!$id) $id=1;
        
        /* Load necessary data for template */
        $template = $this->_setup_page($id);    
        
        /* Load master page view and pass along required template parameters ($template) */
        $this->load->view('master_view', $template);
    }
    
    function _setup_page($id)
    {
        $page_data = $this->page_model->get_page($id);
        $site_data = $this->page_model->get_site_info();
        
        $template['page_title'] = $page_data['page_title'];
        $template['body_class'] = $page_data['body_class'];
        if ($page_data['header_img'] <> '') {$template['header_img'] = base_url().'public/img/'.$page_data['header_img'];} else {$template['header_img'] = '';}
        $template['background_img'] = base_url().'public/img/'.$page_data['background_img'];
        $template['page_headline'] = $page_data['headline'];
        $template['page_headline_img'] = base_url().'public/img/'.$page_data['page_headline_img'];
        $template['page_content'] = $page_data['page_content'];
        $template['footer'] = $site_data['footer'];
        $template['display_featured_prod'] = $page_data['display_featured_product'];
        
        return $template;
    }
    
}

/* End of file page.php */
/* Location: ./system/application/controllers/page.php */

THANKS! rob

#2
[eluser]Dready[/eluser]
Hello,

can you tell us what's not working, or what you trying to achieve ?

#3
[eluser]rt30000[/eluser]
Updated top post. Thx for looking Smile

#4
[eluser]xwero[/eluser]
If .htaccess files are not allowed by the server than there is nothing you can do to remove the index.php.

For the $id as a function argument just make it optional by adding a default value.

#5
[eluser]rt30000[/eluser]
Thank you xwero. I know for a fact I tried a default value without any luck, but I must have had anopther error somewhere. I tried it again and it worked beautifully.

I am still working on getting .htaccess working to remove the index.php, which shouldn't be a huge issue. The biggest thing right now, is understanding how to change the urls from "index.php/page/index/2" to something like "index.php/products". I will be having subdirectories too, such as "index.php/products/cheese" and "index.php/products/cheese/glossary". I am at a loss at how to do this. I looked into something called routing but didn't have much luck. Is that the right method?

Thanks a ton! Im liking CI more and more as I dig deeper into it and mvc concepts.

#6
[eluser]xwero[/eluser]
Yes routing is one way to go. I think products will be a controller but for example the glossary route can be
Code:
$route['(products)/([a-z\-]+)/(glossary)'] = '$1/$3/$2';
$route['(products)/([a-z\-]+)'] = '$1/index/$2';
I catch the segments in the key of the $route array so i don't need to rewrite them in the value part of the $route array. A bonus tip Wink

#7
[eluser]rt30000[/eluser]
Yikes. I need to learn more about regular expressions, etc. That goes way over my head to understand and use it properly.

So do I need a different controller for each page that will have subpages? Right now I just use a single page controller "page.php" (shown at top of this thread) that populates the page with the correct data. Perhaps the next step for me is getting the top-level pages' URL/URI working and displaying correctly, and then attempting the subdirectories? Is this even the best way of setting my site up or am I missing something?

#8
[eluser]rt30000[/eluser]
I think Im making progress! I moved the _setup_page() function to the actual model which makes creating a bunch of controllers not duplicate a ton of code. I think i've made a breakthrough!

http://www.new.gffarms.com/

Now the pages' url shows at least the category they belong in! Now I think I can figure out how to do subpages, they would just be function inside the respective controller (such as "index.php/products/2" could be a cheese page. Right?

I will worry about removing index.php down the road, I dont think thats a major issue. This is restoring faith that I can figure this out.

Rob


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.