dscpg80066 2012-03-20 13:50
浏览 172
已采纳

使用CI(路由)创建动态页面URL - 一个简单的留言板示例

I feel like I have enough experience with CI to finally start fooling around with creating a message board... Or at least thats what I thought, until I got stuck at generating dynamic pages based on subject names (slugs).

So, I creating the controller/model/views and set up a form that submits the necessary info to the database. I pulled the threads out and display them, and generate a link for each one based on the subject title...i essentially followed the CI tutorial, editing it to suit my needs.

However, understanding the concept of generating dynamic page URLs is throwing me off. I understand a lot better by hearing exactly whats happening during the process, and the codeigniter tutorial (news) doesnt explain it well. It simply tells you what to do and how, and not why.

Anyone out there feel up to attempting to explain in greater detail, the process to code dynamic pages.

What I mean by dynamic pages is :

http://your-site.com/news/1/hello-world http://your-site.com/news/1/foo-bar

where hello world and foo bar.

Here are some parts that confuse me:

<?php

class Pages extends CI_Controller {

    public function view($page = 'home')
    {

    }
}

And heres the routing

$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';

what does $1 represent? Any specific url that there? When would you use $2? Is it built in code to CI, or can you use any variable?

I'm sure the answer can get more detailed, but If someone could answer some of the above questions, i'm sure it'd be very helpful.

With those questions answered, in theory, what should be done to produce a new page for a forum thread?

Thanks!

  • 写回答

2条回答 默认 最新

  • douhui8454 2012-03-20 13:58
    关注
    <?php
    
    class Pages extends CI_Controller {
    
        public function view($page = 'home')
        {
    
        }
    }
    

    The corresponding url for this method would be http://example.com/pages/view/. In the method it is set to $page = 'home' because if there is no third segment in the url, it will default to home.

    $route['(:any)'] = 'pages/view/$1';
    

    The variable $1 is whatever you have as your (:any). So if you url is http://example.com/testing, it would route to http://example.com/pages/view/testing and that would in turn set your $page var from your view method to "testing".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向