在Controller

时间:2015-10-28 10:40:07

标签: php codeigniter codeigniter-2 codeigniter-3

在使用AttributeRouting的.NET中,我们可以为每个Action Method添加路由。像下面的东西

[HttpGet, Route("Create-Project")]
public ActionResult CreateProject()
{
     return View();
}

所以,在上面的代码中......第1行表示我们可以提到每个Action Method的路由。所以url会变成下面的东西..

http://domainname/Create-Project

问题

PHP MVC CI是否可行?现在我将不得不在config文件夹中的route.php中编写代码。

3 个答案:

答案 0 :(得分:0)

routes.php中,您可以定义

<强>语法

$route['short_url_name'] = 'complete/path/for/url';

示例

$route['Contact-Us'] = 'home/contact';
$route['Blog'] = 'home/blog';
$route['Our-Work'] = 'home/work';

所以网址看起来像

http://domain.com/Contact-Us
http://domain.com/Blog
http://domain.com/Our-Work

Codeigniter.com examples

答案 1 :(得分:0)

如果您正在寻找数据库驱动的动态路线,您可以这样做,否则您可以按照@abdulla的建议进行操作。

动态路线的表格结构(表名示例:路线)。

id    | slug    |route
---------------------------------------------
1     |Pankaj   |controller/method/id of user
2     |Abdulla  |controller/method/id of user
3     |Niranjan |controller/method/id of user

在控制器中

$this->load->helper('text');
$slug = $this->input->post("username");// example if you want the username in url as route.
$slug = url_title(convert_accented_characters($slug), 'dash', TRUE);
$slug = $this->Routes_model->validate_slug($slug);
$route['slug'] = $slug;
$route_id = $this->Routes_model->save($route);

$route_id将在routes表中具有路由ID,在users table中插入路由ID和slug(对于哪个表,您需要动态路由)

这是路线模型的代码

<?php

class Routes_Model extends CI_Model {

    var $file_name; 
    function __construct()
    {
        parent::__construct();
        $this->file_name = APPPATH.'config/routes'.EXT;
    }

    function check_slug($slug, $id=false)
    {
        if($id)
        {
                $this->db->where('id !=', $id);
        }
        $this->db->where('slug', $slug);
        return (bool) $this->db->count_all_results('routes');
    }

    function validate_slug($slug, $id=false, $count=false)
    {
            if(is_numeric($slug))
            {
               $slug = '';
               $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                for ($i = 0; $i < 3; $i++) 
                {
                        $slug .= $characters[rand(0, strlen($characters) - 1)];
                }
            }
            if($this->check_slug($slug.$count, $id))
            {
                    if(!$count)
                    {
                            $count  = 1;
                    }
                    else
                    {
                            $count++;
                    }
                    return $this->validate_slug($slug, $id, $count);
            }
            else
            {
                    return $slug.$count;
            }
    }

    function save($route)
    {
//        print_r($route);exit;
            if(!empty($route['id']))
            {
                    $this->db->where('id', $route['id']);
                    $this->db->update('routes', $route);
                    return $route['id'];
            }
            else
            {
                    $this->db->insert('routes', $route);
                    return $this->db->insert_id();
            }
    }
}
?>

我检查的一个条件是用户名是否为数字,我会在validate_slug()生成3个字符的长度。

现在,在core文件夹中按文件名MY_Router.php

添加以下代码
    <?php

    class My_Router extends CI_Router 
    {
            function __construct()
            {
                    parent::__construct();
            }

            // this is here to add an additional layer to the routing system.
            //If a route isn't found in the routes config file. then it will scan the database for a matching route.
            function _parse_routes()
            {
                    $segments   = $this->uri->segments;
                    $segments   = array_splice($segments, -2, 2);

                    // Turn the segment array into a URI string
                    $uri = implode('/', $segments);

                    // Is there a literal match?  If so we're done
                    if (isset($this->routes[$uri]))
                    {
                            return $this->_set_request(explode('/', $this->routes[$uri]));
                    }

                    // Loop through the route array looking for wild-cards
                    foreach ($this->routes as $key => $val)
                    {
                            // Convert wild-cards to RegEx
                            $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));

                            // Does the RegEx match?
                            if (preg_match('#^'.$key.'$#', $uri))
                            {
                                    // Do we have a back-reference?
                                    if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
                                    {
                                            $val = preg_replace('#^'.$key.'$#', $val, $uri);
                                    }

                                    return $this->_set_request(explode('/', $val));
                            }
                    }

                    //look through the database for a route that matches and apply the same logic as above :-)
                    //load the database connection information
                    require_once BASEPATH.'database/DB'.EXT;

                    if(count($segments) == 1)
                    {
                            $row    = $this->_get_db_route($segments[0]);

                            if(!empty($row))
                            {
                                    return $this->_set_request(explode('/', $row['route']));
                            }
                    }
                    else
                    {   
                            $segments   = array_reverse($segments);
                            //start with the end just to make sure we're not a multi-tiered category or category/product combo before moving to the second segment
                            //we could stop people from naming products or categories after numbers, but that would be limiting their use.
                            $row    = $this->_get_db_route($segments[0]);
                            //set a pagination flag. If this is set true in the next if statement we'll know that the first row is segment is possibly a page number
                            $page_flag  = false;
                            if($row)
                            {
                                    return $this->_set_request(explode('/', $row['route']));
                            }
                            else
                            {
                                    //this is the second go
                                    $row    = $this->_get_db_route($segments[1]);
                                    $page_flag  = true;
                            }

                            //we have a hit, continue down the path!
                            if($row)
                            {
                                    if(!$page_flag)
                                    {
                                            return $this->_set_request(explode('/', $row['route']));
                                    }
                                    else
                                    {
                                            $key = $row['slug'].'/([0-9]+)';

                                            //pages can only be numerical. This could end in a mighty big error!!!!
                                            if (preg_match('#^'.$key.'$#', $uri))
                                            {
                                                    $row['route'] = preg_replace('#^'.$key.'$#', $row['route'],$uri);
                                                    return $this->_set_request(explode('/', $row['route']));
                                            }
                                    }
                            }
                    }

                    // If we got this far it means we didn't encounter a
                    // matching route so we'll set the site default route
                    $this->_set_request($this->uri->segments);
            }

            function _get_db_route($slug)
            {
                    return DB()->where('slug',$slug)->get('routes')->row_array();
            }
    }

以上文件非常重要。由于codeigniter首先在core文件夹中运行文件,这将创建你想要的slug。无需在routes.php文件中添加任何路由。

现在,在显示用户时,你会在用户表中回显一下slu。

假设控制器名称为users,方法名称为edit_user

您的控制器代码将如下所示,

function edit_user($id){

      // fetch the data related to $id, and load the view here
}

在网址中,如果它看到Niranjan,它会自动从routes表中选择路线并输入您的功能edit_user()

是方法看起来很长,但效果很好,我在所有电子商务网站都使用此功能。

答案 2 :(得分:0)

Application / config / router.php 中,您可以添加以下配置:

$route['Create-Project'] = 'home/welcome';

因此,每次访问 http://domain.com/Create-Project 时,都会将您重定向到 http://domain.com/home/welcome