简体   繁体   English

如何使用codeigniter更改URL中的函数名称?

[英]how to change the function name in the url using codeigniter?

I want to change the function name in the url, i searched for this a lot but unable to find any solution, can any one help me out what should i do consider for example i have a url like 我想更改网址中的函数名称,我进行了很多搜索,但找不到任何解决方案,有人可以帮助我,我应该考虑些什么,例如我有一个网址,例如

http://localhost/codeIgniter_try/index.php/Blog/blogvieww , http://localhost/codeIgniter_try/index.php/Blog/blogvieww

so here "Blog" is the controller name and "blogvieww" is the function name so if i want to change the function name from "blogvieww" to "blogvieww_all" what can i do? 所以这里“ Blog”是控制器名称,“ blogvieww”是函数名称,所以如果我想将函数名称从“ blogvieww”更改为“ blogvieww_all”,我该怎么办?

Blog.php Blog.php

   <?php  
        defined('BASEPATH') OR exit('No direct script access allowed');  

        class Blog extends CI_Controller {  

            public function index()  
            {  
                $this->load->view('blogview');  
            }

            public function blogvieww()  
            {  
                $this->load->view('blogvieww');  
            }
        }  
    ?>

blogview.php blogview.php

    <html>
        <head>
                <title>My Blog</title>
        </head>
        <body>
            <div>
                <h1>Welcome to my 1st Blog!</h1>
            </div>    
        </body>
    </html>

blogvieww.php blogvieww.php

    <html>
        <head>
                <title>My Blog</title>
        </head>
        <body>
            <div>
                <h1>Welcome to my 2nd Blog!</h1>
            </div>

            <div>
                <h1>Welcome to my 3rd Blog!</h1>
            </div>
        </body>
    </html>

You can set it through routes of CodeIgniter. 您可以通过CodeIgniter的routes进行设置。

Your path for routes will be application/config/routes.php 您的routes将为application / config / routes.php

See this below may help. 参见下面的内容可能会有所帮助。

$route['Blog/blogvieww_all'] = 'Blog/blogvieww';

For more detail: https://www.codeigniter.com/userguide3/general/routing.html 有关更多详细信息: https : //www.codeigniter.com/userguide3/general/routing.html

Hope this will help you : 希望这个能对您有所帮助 :

Add the below line of code in your route.php route.php添加以下代码行

$route['Blog/blogvieww_all'] = 'Blog/blogvieww';

Your anchor should be like this : 您的锚点应如下所示:

<a href=<?=site_url('Blog/blogvieww_all');?>

1) You can use URI routes. 1)您可以使用URI路由。 In routes.php, You can specify like below: 在routes.php中,您可以如下指定:

$route['Blog/blogvieww_all'] = 'Blog/blogvieww';

Check here for more info. 在这里查看更多信息。

2) Write same function again in controller with the name of 'blogvieww_all' . 2)在控制器中再次使用“ blogvieww_all”的名称编写相同的功能。

Change this in your controller: 在您的控制器中更改此设置:

   public function blogvieww()  
        {  
            $this->load->view('blogvieww');  
        }

to

   public function blogviewW_all()  
        {  
            $this->load->view('blogvieww');  
        }

You just need to change the name of the function in your controller, to the name you want to display on ur url. 您只需要将控制器中函数的名称更改为要在url上显示的名称即可。

Or 要么

As Pradeep has commented, you can change to routing also. 正如Pradeep所说,您也可以更改为路由。 But the best way is to change the function name, unless it is being referenced or called from somewhere else. 但是最好的方法是更改​​函数名,除非从其他地方引用或调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM