Ahmed Nabil
Ahmed Nabil

Reputation: 18996

How to identify Routing rule that used that directed to my Codeigniter controller?

In my Application
I have 3 routing rules (defined in routes.php file) thats directed to the one controller

$route[urlencode('news')]       = "news/show-news";  
$route[urlencode('letters')]    = "news/show-news";  
$route[urlencode('papers')]     = "news/show-news"; 

so
if any user navigate to any of those URLs
http://example.com/news/
http://example.com/letters/
http://example.com/papers/

The target controller will be news/show-news

My need
How to identify Routing rule that used that directed to my Codeigniter controller?

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class News extends CI_Controller {
    public function show-news(){        
        // came from [news OR letters OR papers]  ??

    }   
}

Upvotes: 2

Views: 119

Answers (3)

Ahmed Nabil
Ahmed Nabil

Reputation: 18996

First thanks to @aspirinemaga
His answer is correct
But if we want to do that without using parametric controller function
We can do that

public function show-news(){

    // came from [news OR letters OR papers]  ??
    $requested_URI_segment = $this->uri->segment(1);
    $requested_URI_segment = urldecode($requested_URI_segment);

    switch ( $requested_URI_segment ){
        // If news requested
        case 'news':
            // your code here
            echo 'news requested'. '</br>';
        break;

        // If letters requested
        case 'letters':
            // your code here
            echo 'letters requested'. '</br>';
        break;

        // If papers requested
        case 'papers':
            // your code here
            echo 'papers requested'. '</br>';
        break;

        // Default landing if nothing provided
        default:
            // your code here
            echo 'select one of the show method'. '</br>';

    }

Upvotes: 1

aspirinemaga
aspirinemaga

Reputation: 3937

You can do it with something like this, but there are various methods to achieve what you want. Basically, to give you a kick-start, check out the code below:

In your controller, you put this code:

class News extends CI_Controller {
    public function show_news( $method = '' )      
    {

        switch ( $method )
        {
            // If news requested
            case 'news':
                // your code here
                echo 'news requested';
            break;

            // If letters requested
            case 'letters':
                // your code here
                echo 'letters requested';
            break;

            // If papers requested
            case 'papers':
                // your code here
                echo 'papers requested';
            break;

            // Default landing if nothing provided
            default:
                // your code here
                echo 'select one of the show method';

        }

    }   
}

And in your routes.php, you type this:

$route['news'] = "news/show_news/news";
$route['letters'] = "news/show_news/letters";
$route['papers'] = "news/show_news/papers";

So you have a 4th option, that if nothing is provided, you can redirect to some page where the users can choose one of the available types of news in the default section.

Upvotes: 1

Hendra Nucleo
Hendra Nucleo

Reputation: 591

Try this :

$route['news'] = "controller/method_name";
$route['letter'] = "controller/method_name";
$route['papers'] = "controller/method_name";

I have project which i made using CI before, and i have section which is for student & absent. So in my route i set like this :

$route['student/page'] = "student/index";
$route['student/page/(:num)'] = "student/index/$1";
$route['absent/page'] = "absent/index";
$route['absent/page/(:num)'] = "absent/index/$1";

And with some setting on htaccess.

Upvotes: -2

Related Questions