Welcome Guest, Not a member yet? Register   Sign In
Remove 'segments' from url
#1

[eluser]Jakob Pedersen[/eluser]
Hi! :-)

First of all this looks like a fantastic community and I simply love how simple and straightforward CodeIgniter actually is (I'm coming from a heavily moded Joomla site, which was turning into a nightmare)

In the last couple of days I've worked intensely with creating my own CMS with a backend interface, but I have to issues/hurdles I want to overcome:

1) I would love if my content urls (with articles) were much simpler.

Right now they follow this pattern:

domain.com/content/comments/this-is-a-blog-title

Content is obviously the controller and comments a function in the controller, but I would love if I could remove these to segments from the url when calling excactly that controller to this:

domain.com/this-is-a-blog-title - especially because that's the way it's on my current Joomla site.

But on every other controller the URL has to be normal and follow the usual pattern:

domain.com/controller/function

2) Right know the only way to link to an article is to do it with the comment view, but that doesn't include the article -any easy way to combine them, so you don't have to go to different pages?

Thank you very much, and sorry if you have answered this before - I really have searched for a solution.
#2

[eluser]pistolPete[/eluser]
Have a look at URI Routing:

- http://ellislab.com/codeigniter/user-gui...uting.html
- http://ellislab.com/forums/viewthread/107394/
#3

[eluser]Jakob Pedersen[/eluser]
[quote author="pistolPete" date="1267456755"]Have a look at URI Routing:

- http://ellislab.com/codeigniter/user-gui...uting.html
- http://ellislab.com/forums/viewthread/107394/[/quote]
Thanks, but I've tried that several times and can't get it working probably.

If I put this in the routing config:

Code:
$route[':any'] = 'content/comments/:any';
It does redirect to the content controller and function - but is not looking on the third segment, so I get all comments on one page (same as calling content/comments without any slug)

Furthermore the other controllers don't work any longer (probably sort of a given, since it routes everything to content/comments) but I've got no clue on how to only enable the routing for the content controller including the third uri segment..
#4

[eluser]pistolPete[/eluser]
You have to use the correct syntax as described in the user guide:

Code:
$route['(:any)'] = 'content/comments/$1';

Quote:Furthermore the other controllers don’t work any longer...
Read this: http://ellislab.com/forums/viewreply/540633/
#5

[eluser]stef25[/eluser]
CI's routing feature is excellent, allows you to use pretty much any URL you want. I'm using it in a large app and adding one line to the routing file for every "section" of your site is really not that much work.
#6

[eluser]Jakob Pedersen[/eluser]
[quote author="stef25" date="1267467275"]CI's routing feature is excellent, allows you to use pretty much any URL you want. I'm using it in a large app and adding one line to the routing file for every "section" of your site is really not that much work.[/quote]
Yeah that would be perfectly fine, but this code:

Code:
$route['(:any)'] = 'content/comments/$1';

still redirects only to the function, not the specific article (defined with a slug table)
#7

[eluser]pistolPete[/eluser]
Can you post the content controller?

You could also use this route:

Code:
$route[':any'] = 'content/comments/';

and get the segment using

Code:
$this->uri->segment(1)
#8

[eluser]Jakob Pedersen[/eluser]
I changed the anchor to use the segment-1 - so the article work now, but I'm not able to comment:


content.php [controller]

Code:
<?php

    class Content extends Controller
    {
    
        function Content()
        {
            parent::Controller();
            
            $this->load->helper('url');
            $this->load->helper('form');
            
        }
    
        function index()
        {
            $this->load->model('articles');
            $data['main_content'] = 'content';
            $data['title'] = 'Seneste artikler';
            $data['records'] = $this->articles->getAll();
            $this->load->view('includes/master', $data);
        }
        
        function comments()
        {
            $data['main_content'] = 'comment_view';
            $this->db->where('slug', $this->uri->segment(1));
            $data['query'] = $this->db->get('comments');
            
            $this->load->view('includes/master', $data);
        }
        
        function comment_insert()
        {
            $this->load->library('form_validation');
            // felter, fejl og regler
            $this->form_validation->set_rules('author', 'navn', 'trim|required');
            $this->form_validation->set_rules('text', 'tekst', 'trim|required');
            
            if ($this->form_validation->run() == FALSE)
                    {
                        echo 'Validationen fejlede! Husk at få lavet ordentligt view, i stedet for dette miskmask!';
                    }
                    else
                    {
                        $this->db->insert('comments', $_POST);
                        redirect('content/comments/' .$_POST['slug']);
                    }
        }
    }
?>

content.php [view]

Code:
<div id="container">
            <h1>Mit view!!</h1>
        
            &lt;?php foreach($records as $row) : ?&gt;
            <h2>&lt;?php echo $row->title; ?&gt;</h2>
            &lt;?php echo $row->text; ?&gt;
            
            <p>&lt;?php echo anchor('' .url_title(strtolower($row->title)), 'Kommentarer'); ?&gt; </p>
            
            <hr />
            &lt;?php endforeach; ?&gt;    
        
        </div>

comment_view.php [view]

Code:
<div id="container">
            <h1>Mit view!!</h1>
        
            &lt;?php foreach($records as $row) : ?&gt;
            <h2>&lt;?php echo $row->title; ?&gt;</h2>
            &lt;?php echo $row->text; ?&gt;
            
            <p>&lt;?php echo anchor(.url_title(strtolower($row->title)), 'Kommentarer'); ?&gt; </p>
            
            <hr />
            &lt;?php endforeach; ?&gt;    
        
        </div>

Structure:

views:
..content.php
..comment_view




Theme © iAndrew 2016 - Forum software by © MyBB