在网址中隐藏codeigniter控制器和方法名称

编程入门 行业动态 更新时间:2024-10-17 02:50:34
本文介绍了在网址中隐藏codeigniter控制器和方法名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的网址是这样的: example/controller/method/param1/param2/param3/param4

我的目标是: example/param1/param2/param3/param4

在这里,我的控制器为"Home"和"Read".我还在底部包括了我的视图和路由配置.

Here my controllers 'Home' and 'Read'. I also include my view and route configuration at bottom.

<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Home extends CI_Controller { public function __construct() { parent::__construct(); } /* * Index method */ public function index() { $this->db->select('a.*, c.*'); $this->db->join('category as c', 'c.catid=a.catid'); $this->db->limit(10); $this->db->order_by('a.id', 'DESC'); $data['query'] = $this->db->get('article as a'); $this->load->view('header'); $this->load->view('home_view', $data); $this->load->view('footer'); } } <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Read extends CI_Controller { public function __construct() { parent::__construct(); } /* * Index method */ public function index() { $id = isset($this->uri->segment(3)) ? $this->uri->segment(3) : FALSE; $this->db->select('a.*, c.*'); $this->db->join('category as c', 'c.catid=a.catid'); $this->db->where('p.id', $id); $this->db->limit(1); $data['query'] = $this->db->get('article as a'); $this->load->view('header'); $this->load->view('single_view', $data); $this->load->view('footer'); } }

home_view.php

home_view.php

请在下面查看锚点,这是我生成 example/param1/param2/param3/param4

Please take a look at anchor below, that was my goal to produce example/param1/param2/param3/param4

<div class="post-content"> <ul> <?php foreach( $query->result() as $post ) : ?> <li><a href="<?php echo site_url($post->term_name.'/'.$post->term_id.'/'.$post->id.'/'.strtolower(url_title($post->title))); ?>"><?php echo $post->title ?></a></li> <?php endforeach; ?> </ul> </div>

当我定义控制器名称时,没问题.但是我不知道我的单个帖子的URL如何仅包含这四个参数

When I defined the controller name, it's no problem. But I can't figure out how my url for single post only contain those four params

<?php echo site_url('read/'.$post->term_name.'/'.$post->term_id.'/'.$post->id.'/'.strtolower(url_title($post->title))); ?>

在我的路由配置下面:

$route['(:any)'] = "read/index/$1";

任何帮助和建议将不胜感激.

Any help and advices will be appreciated.

最好的问候

推荐答案

检查可放置下一条路线的文档:

Checking the docs you can place next route:

$route['(:any)/(:any)/(:any)/(:any)'] = 'controller/method/$1/$2/$3/$4'; $route['(:any)/(:any)/(:any)'] = 'controller/method/$1/$2/$3'; $route['(:any)/(:any)'] = 'controller/method/$1/$2';

或者如果您要传递数字/整数

or if you are passing numbers/integers

$route['(:num)/(:num)/(:num)/(:num)'] = 'controller/method/$1/$2/$3/$4';

您也不要忘记将参数传递给您的方法,即使是FALSE也是如此.

You mustn't forget to pass arguments to your method as well, even as FALSE.

public function index($param1, $param2, $param3, $param4) { //rest of code that is using params }

更多推荐

在网址中隐藏codeigniter控制器和方法名称

本文发布于:2023-10-31 13:26:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546216.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   名称   网址   方法   codeigniter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!