CodeIgniter Help

Jake232

New member
Sep 4, 2010
712
4
0
The CI forum is being annoying, and won't load for me, and I know some of you use this framework, I started learning it yesterday, and don't quite understand how URL's work, even after reading the userguide.

I have http://localhost:8888/cms/site/posts/

site being the controller, posts being the method/function within. I want to have a URL structure such as

http://localhost:8888/cms/site/posts/edit/2

I tried adding the edit function WITHIN the posts function, however it doesn't work and simply just runs the post function as normal. I was wondering if anyone could help?

Site Controller:
Code:
<?php

class Site extends CI_Controller{
	
	function __construct(){
		parent::__construct();
		// See if session set
		$this->is_logged_in();
	}
	
	function index(){
		// Show members area
		$data['main_content'] = 'members/members_area';
		$this->load->view('includes/template',$data);
	}
	function posts(){
		
		$this->load->model('posts_model');
		$this->load->library('table');
		$this->table->set_heading(array('Title', 'Author','Category'));
		$data['posts'] = $this->posts_model->get_posts();
		
		// Show members area
		$data['main_content'] = 'members/posts';
		$this->load->view('includes/template',$data);
		
		// I put this WITHIN the posts function to try and get the URL structure correct
		function edit(){
			$this->load->model('posts_model');
			$data['post'] = $this->posts_model->get_a_post($this->uri->segment(4));
			$data['main_content'] = 'members/posts_edi1t';
			$this->load->view('includes/template',$data);
		}
		
		
	}
	
	

	
	function is_logged_in(){
		$is_logged_in = $this->session->userdata('is_logged_in');
		if(!isset($is_logged_in) || $is_logged_in != true){
			echo 'You don\'t have access';
			die();
		}
	}
	
}
 


If you are new to CI, I would strongly recommend going through the user guide (controller explanation), as it is very detailed (some of the best documentation for any framework).

With that said, it works like this:

http://mysite.com/controller/method/argument1/argument2

So in your case, you should have a controller called "posts", with a method called "edit" which takes 1 argument like this:
Code:
class Posts extends CI_Controller{
	
	function __construct(){
		parent::__construct();
		// See if session set
		$this->is_logged_in();
	}
        
        function edit($post_id) {
          // Do the post edit...
        }
}
 
Thanks dchuk, got it working with

Code:
$route['admin/posts'] = "posts";
$route['admin/posts/(:any)'] = "posts/$1";

I'm not sure if thats the correct way of routing it, but it seems to work. ;P

+Rep added.