doudao8283 2016-03-23 07:12
浏览 24

如何最好地实现数据库驱动的动态URL /控制器名称?

I have a website that is written in PHP on codeigniter. The scenario I am looking to cope with is the following:

When a user signs up, Their personalised homepage should appear at www.mysite.com/username. Other urls are of the form www.mysite.com/username/pictures & www.mysite.com/username/videos.

So basically what I need is www.mysite.com/username/method (where username is database driven) should always hit the same controller, if the username exists in the database.

Currently, what I am doing is I am using the custom 404 controller to do this. If a URL reaches the custom 404 controller, I check if the controller name matches a username in the data base, then checking the method name and calling the required method. Like so:

$this->load->model('school_model');
        $this->load->model('event_model');

        $this->load->helper('apps_helper');

        $controllerName = $this->uri->segment(CONTROLLER);
        $controllerMatch = $this->school_model->findSchoolByUid($controllerName);
        $this->data['controller'] = $controllerMatch;

        if($controllerMatch != false){
            $methodName = $this->uri->segment(METHOD);
            if($methodName === "something"){
            //Do something
            }
            if($methodName === "something else"){
            //Do something else
            }
            if($methodName === "another thing"){
            //Do another thing
            }
            if($methodName === "last thing"){
            //Do last thing
            }
            ...
        }
        else{
            //Load 404 page
            $this->output->set_status_header('404');
            $data['content404'] = true; 
            $this->load->view("common/header", $data);
            $this->load->view('frontend/index404', $this->data);
            $this->load->view("common/footer");
        }

My query is, is this the best way to go about what I need ? How can I improve this ? I have heard of the HMVC modular extension for codeiginiter which may be a good bet ? Just looking for some advice.

  • 写回答

1条回答 默认 最新

  • dongshan1959 2016-03-23 12:02
    关注

    You can do this by writing this code in your config > routs.php file

    $route['(:any)/pictures'] = 'yourcontrollername/functionName/$1';
    

    now in this if any one write username/pictures url than it will automatically goes to your controller.

    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝