Welcome Guest, Not a member yet? Register   Sign In
Help with planning a CI project
#1

[eluser]invision[/eluser]
Hi,

I'm planning to build a small site for a business and would really love to do it in CodeIgniter.

It's a fairly basic site which, in time, I hope to content-manage with a CMS.

My Site Map:
Quote:Home
About
- Our Team
- Location
Vacancies
News
Contact

Now, I just wonder how the best way of organising this in CodeIgniter would be?

Home and About pages would just be taking static content from a 'pages' table in my database and outputting it.
However, Vacancies and News would list data from the vacancies and news tables respectively.

Would I create models for Pages, Vacancies and News.
And then create a Controller for each of these too?

Ultimately, I'd love my web url's to be like:
www.example.com/about/our-team
www.example.com/news/news-entry-slug

If anyone can give me a head start or any pointers, I'd super appreciate it.


Many thanks.
#2

[eluser]eoinmcg[/eluser]
I'd do it with 3 controllers:

pages as default controller
news controller
vacancies controller

the latter 2 are fairly self explanatory. but pages may need a bit more of a breakdown:
1. in routes.php set default controller as so
Code:
$route['default_controller'] = "pages";
$route['pages/:any'] = "pages";
2. the pages index method checks how the uri segments and loads the appropriate page from the db
3. to get rid of http://www.example.com/pages/about/etc/etc you'll need to modify the router class, specifically the _validate_request(), method. there's plenty of example on the forum about this but give me a shout if you get stuck.

good luck!
#3

[eluser]invision[/eluser]
Many thanks for the speedy reply eoinmcg!

Prepare yourself for the response Smile

Just to get me rolling....how does the following look:

Code:
// Page Controller
class Page extends Controller {
  function Page(){
    parent::Controller();
    session_start();
  }

  function index(){
    // say 'home' is the value of the slug in my sql table
    $data['page_data'] = $this->MPages->getPageBySlug('home');
    $data['title'] = "Welcome";
    $data['main'] = 'public_home';
    $this->load->vars($data);
    $this->load->view('template');  
  }
  
  function page($slug){
    $data['page_data'] = $this->MPages->getPageBySlug($slug);
    $data['title'] = $data['page']['title'];
    $data['main'] = 'public_page';
    $this->load->vars($data);
    $this->load->view('template');  
  }



// Vacancies Controller
class Vacancies extends Controller {
  function Vacancies(){
    parent::Controller();
    session_start();
  }

  function index(){
    $data['page_data'] = $this->MVacancies->getVacancies();
    $data['title'] = "Vacancies";
    $data['main'] = 'public_home';
    $this->load->vars($data);
    $this->load->view('template');  
  }
  
  function vacancy($slug){
    $data['page_data'] = $this->MVacancies->getVacancyBySlug($slug);
    $data['title'] = $data['page']['title'];
    $data['main'] = 'public_page';
    $this->load->vars($data);
    $this->load->view('template');  
  }



// News Controller
class Vacancies extends Controller {
  function Vacancies(){
    parent::Controller();
    session_start();
  }

  function index(){
    $data['page_data'] = $this->MNews->getNews();
    $data['title'] = "News";
    $data['main'] = 'public_home';
    $this->load->vars($data);
    $this->load->view('template');  
  }
  
  function news($slug){
    $data['page_data'] = $this->MNews->getNewsBySlug($slug);
    $data['title'] = $data['page']['title'];
    $data['main'] = 'public_page';
    $this->load->vars($data);
    $this->load->view('template');  
  }

Models

Code:
// Pages Model
class MPages extends Model{

function MPages(){
    parent::Model();
}

function getPage($id){
    $data = array();
    $this->db->where('id',$id);
    $this->db->limit(1);
    $Q = $this->db->get('pages');
    if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }
    $Q->free_result();    
    return $data;    
}

function getPageBySlug($slug){
    $data = array();
    $this->db->where('slug',$slug);
    $this->db->limit(1);
    $Q = $this->db->get('pages');
    if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }
    $Q->free_result();    
    return $data;    
}




// Vacancies Model
class MVacancies extends Model{

function MVacancies(){
    parent::Model();
}

function getVacancies(){
     $data = array();
     $Q = $this->db->get('vacancies');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[] = $row;
       }
    }
    $Q->free_result();  
    return $data;
}




// News Model
class MNews extends Model{

function MNews(){
    parent::Model();
}

function getNews(){
     $data = array();
     $Q = $this->db->get('news');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[] = $row;
       }
    }
    $Q->free_result();  
    return $data;
}

Views
Code:
// Vacancies View
if (count($posts)){
    foreach ($posts as $key => $list){
        echo "<h2>".$list['title']."</h2>\n";
        echo auto_typography(word_limiter($list['body'], 200));
        echo anchor('blog/page/'.$list['id'],'read more >>');
    }
    echo "<br/><br/>";
}



// News View
if (count($posts)){
    foreach ($posts as $key => $list){
        echo "<h2>".$list['title']."</h2>\n";
        echo auto_typography(word_limiter($list['body'], 200));
        echo anchor('blog/page/'.$list['id'],'read more >>');
    }
    echo "<br/><br/>";
}



// Page View
echo auto_typography($post['title']);
echo auto_typography($post['body']);

Template View would just be something like:
Code:
<div id="mainContent">
   &lt;?php $this->load->view($main);?&gt;
</div>


Does this look like a good start?

I realise it's a lot to take in at first, but am I heading in the right direction?

I really value any feedback for this. Very new to CodeIgniter but excited for the project.


Thank you.
#4

[eluser]eoinmcg[/eluser]
sure, looks fine, after a quick glance....

experience is the best teacher, so get hacking!
#5

[eluser]invision[/eluser]
Eeeeek. Scared in a good way Big Grin

Busy night ahead for me.

I may be back. Thanks again.
#6

[eluser]invision[/eluser]
So far, so-so Smile

I'm going to focus on Pages in this reply.

Controller:
Code:
&lt;?php
class Page extends Controller {
  function Page(){
    parent::Controller();
    session_start();
  }

  function index(){
    // say 'home' is the value of the slug in my sql table
    $data['page_data'] = $this->MPages->getPageBySlug('home');
    $data['title'] = "Welcome";
    $data['main'] = 'public_home';
    $this->load->vars($data);
    $this->load->view('template');  
  }
  
  function pages($slug){
    $data['page_data'] = $this->MPages->getPageBySlug($slug);
    $data['title'] = $data['page']['title'];
    $data['main'] = 'public_page';
    $this->load->vars($data);
    $this->load->view('template');  
  }
  
}

Model:
Code:
&lt;?php
class MPages extends Model{

    function MPages(){
        parent::Model();
    }
    
    function getPageBySlug($slug){
        $data = array();
        $this->db->where('slug',$slug);
        $this->db->limit(1);
        $Q = $this->db->get('pages');
        if ($Q->num_rows() > 0){
          $data = $Q->row_array();
        }
        $Q->free_result();    
        return $data;    
    }

}

Now going to: http://www.site.com/index.php/page/ shows my Home Page perfectly.

However I have created a page 'Location' as a record in my 'pages' table.
I was hoping to visit this by going to http://www.site.com/index.php/page/about-us/location/ but sadly this doesn't work. I get a 404 Error.

Could someone guide me where I'm going wrong with the above code?
I'm new to this, so as much help as you can give is so much appreciated.


Many thanks.
#7

[eluser]theprodigy[/eluser]
Code:
function pages($slug){
    $data['page_data'] = $this->MPages->getPageBySlug($slug);
    $data['title'] = $data['page']['title'];
    $data['main'] = 'public_page';
    $this->load->vars($data);
    $this->load->view('template');  
  }
the above function is looking for a single parameter.
Your url of http://www.site.com/index.php/page/about-us/location/ is passing about-us into that parameter. So your model is looking for the record that has about-us as the slug

EDIT: Actually, I'm incorrect in the above. Sorry.
Your default is working because it's calling the index function.
In order to get the above method to work, you would need a url of something like http://www.site.com/index.php/page/pages/location/. You would need to call the method, then pass in the parameter
#8

[eluser]invision[/eluser]
Aaaah good call.

However, I still get a 404 Error when I try to load 'about-us' on it's own. 'about-us' is the value of a slug in my sql table.

Would I need a new function if I was to retrieve 2 slugs?

Or would it make more sense just to retrieve the ID of the record and rewrite the URL some other way?

Thanks
#9

[eluser]theprodigy[/eluser]
I just editted my reply. Make sure you refresh you page ;-)
#10

[eluser]invision[/eluser]
Wow, got it in one Smile Thank you for your help.

It's now displaying under:
http://www.site.com/index.php/page/pages/location/

However, I'd like to change this to be:
http://www.site.com/index.php/page/about-us/location/


Would this be an easy fix to make?




Theme © iAndrew 2016 - Forum software by © MyBB