Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess 从产品名称到产品id的Codeigniter路由URL_.htaccess_Codeigniter - Fatal编程技术网

.htaccess 从产品名称到产品id的Codeigniter路由URL

.htaccess 从产品名称到产品id的Codeigniter路由URL,.htaccess,codeigniter,.htaccess,Codeigniter,我需要像这样重定向URLhttp://mysite.com/store/store-name至http://mysite.com/stores/products/store-id。注意,我需要从数据库中获取存储id。那么可以在routes.php中执行db操作吗 在文档中,语法为$route['store/:any']。如何获取此处提到的第二个参数的值,即:any是的,这很简单,您只需要显示ID而不是名称, 您必须执行类似storeName>单击查看详细信息的操作 使之成为 storeId>单击

我需要像这样重定向URL
http://mysite.com/store/store-name
http://mysite.com/stores/products/store-id
。注意,我需要从数据库中获取存储id。那么可以在routes.php中执行db操作吗


在文档中,语法为
$route['store/:any']
。如何获取此处提到的第二个参数的值,即
:any

是的,这很简单,您只需要显示ID而不是名称, 您必须执行类似storeName>单击查看详细信息的操作

使之成为

storeId>单击以查看详细信息

当您将参数传递给数据库时,更改mysql的检查,将其更改为id而不是name,这类似于 “从id=“.parameter”的表_name中选择您所需的列。”


谢谢

是的,这很简单,你只需要显示ID而不是姓名, 您必须执行类似storeName>单击查看详细信息的操作

使之成为

storeId>单击以查看详细信息

当您将参数传递给数据库时,更改mysql的检查,将其更改为id而不是name,这类似于 “从id=“.parameter”的表_name中选择您所需的列。”


谢谢

通过路由运行数据库查询实际上没有任何好的或简单的方法。但是,您可以在控制器函数的开头进行验证

我想你的
店名
是该产品的一部分吗?基本上,您可以验证值是否为数字,如果不是,则通过slug查找,然后重定向

config/routes.php

$route["store/(.*)"] = 'stores/products/$1';
/* () and $1 together passes the values */
/* Class etc. */

function products($mix) {

    if (is_numeric($mix))
        $int_id = $mix;
    else {

        $row = $this->get_where('products', array('slug' => $mix))->row();

        $this->load->helper('url');
        redirect("stores/products/{$row->id}");

    }

    /* Do stuff with the $int_id */

}
controllers/stores.php

$route["store/(.*)"] = 'stores/products/$1';
/* () and $1 together passes the values */
/* Class etc. */

function products($mix) {

    if (is_numeric($mix))
        $int_id = $mix;
    else {

        $row = $this->get_where('products', array('slug' => $mix))->row();

        $this->load->helper('url');
        redirect("stores/products/{$row->id}");

    }

    /* Do stuff with the $int_id */

}
这表明您有:

  • 名为产品的表
  • 名为id的列是您的产品id
  • 一个名为slug的列,该列基于您的
    商店名称

通过路由运行数据库查询实际上没有任何好的或简单的方法。但是,您可以在控制器函数的开头进行验证

我想你的
店名
是该产品的一部分吗?基本上,您可以验证值是否为数字,如果不是,则通过slug查找,然后重定向

config/routes.php

$route["store/(.*)"] = 'stores/products/$1';
/* () and $1 together passes the values */
/* Class etc. */

function products($mix) {

    if (is_numeric($mix))
        $int_id = $mix;
    else {

        $row = $this->get_where('products', array('slug' => $mix))->row();

        $this->load->helper('url');
        redirect("stores/products/{$row->id}");

    }

    /* Do stuff with the $int_id */

}
controllers/stores.php

$route["store/(.*)"] = 'stores/products/$1';
/* () and $1 together passes the values */
/* Class etc. */

function products($mix) {

    if (is_numeric($mix))
        $int_id = $mix;
    else {

        $row = $this->get_where('products', array('slug' => $mix))->row();

        $this->load->helper('url');
        redirect("stores/products/{$row->id}");

    }

    /* Do stuff with the $int_id */

}
这表明您有:

  • 名为产品的表
  • 名为id的列是您的产品id
  • 一个名为slug的列,该列基于您的
    商店名称

    • 我参加聚会可能会晚一点,但我可能有其他建议

      我使用以下路线:

      http://mysite.com/store/1/store-name
      
      原因是。。。根据您的方法,如果您创建

      http://mysite.com/store/store-name
      
      但过了一段时间后(毫无疑问谷歌已经为你的页面编制了索引),你决定出于什么原因必须将商店名称更改为“精彩商店名称”,你自然会将链接更改为

      http://mysite.com/store/wonderful-store-name
      
      这会扼杀你的搜索引擎优化和任何索引链接


      我使用的解决方案是,您可以将
      商店名称更改为您想要的任何内容,但它将始终引用
      1
      ,这意味着用户仍将看到相关页面。

      我可能会晚一点参加聚会,但我可能有其他建议

      我使用以下路线:

      http://mysite.com/store/1/store-name
      
      原因是。。。根据您的方法,如果您创建

      http://mysite.com/store/store-name
      
      但过了一段时间后(毫无疑问谷歌已经为你的页面编制了索引),你决定出于什么原因必须将商店名称更改为“精彩商店名称”,你自然会将链接更改为

      http://mysite.com/store/wonderful-store-name
      
      这会扼杀你的搜索引擎优化和任何索引链接

      我使用的解决方案意味着您可以将
      商店名称更改为您想要的任何内容,但它将始终引用
      1
      ,这意味着用户仍将看到相关页面。

      使用CodeIgniter routes可以实现任何功能。这一切都取决于你编写代码的方式。CI中的路由非常灵活。除了标准CI通配符(:any)(:num)之外,还可以使用正则表达式。如果需要,您甚至可以向路径变量添加前缀或后缀:

      $route['store/(:any)'] = "redircontroller/redirfunction/$1";
      // for instance the namelookup method of the mystores controller
      $route['stores/products/(:any)'] = "mystores/namelookup/$1";
      
      通过在路由值中定义传递给所定义的控制器方法的变量,可以获得第二个参数(以及第三个参数等等)。如果新url中的“products”也是一个变体,则应改为在此处启动通配符表达式。您还可以使用URI类($this->URI->segment(n))从url中提取参数

      但是,您不能在routes.php中执行数据库操作。您在路由到的控制器中执行数据库操作。我的猜测是,您必须在查询中使用url中使用的任何内容来匹配存储id。 在任何情况下,您使用路由文件的路径都是用户将看到的路径。要执行重定向,您必须接受原始路径,然后将用户重定向到新路径,如下所示:

      // in some controller that's attached to the original url
          public function redirfunct($var){
              $this->load->helper('url');
              redirect(base_url('stores/products/' . $var));
          }
      
      我希望这对您有所帮助。

      使用CodeIgniter路由,任何事情都是可能的。这一切都取决于你编写代码的方式。CI中的路由非常灵活。除了标准CI通配符(:any)(:num)之外,还可以使用正则表达式。如果需要,您甚至可以向路径变量添加前缀或后缀:

      $route['store/(:any)'] = "redircontroller/redirfunction/$1";
      // for instance the namelookup method of the mystores controller
      $route['stores/products/(:any)'] = "mystores/namelookup/$1";
      
      通过在路由值中定义传递给所定义的控制器方法的变量,可以获得第二个参数(以及第三个参数等等)。如果新url中的“products”也是一个变体,则应改为在此处启动通配符表达式。您还可以使用URI类($this->URI->segment(n))从url中提取参数

      但是,您不能在routes.php中执行数据库操作。您在路由到的控制器中执行数据库操作。我的猜测是,您必须在查询中使用url中使用的任何内容来匹配存储id。 在里面