简体   繁体   English

codeigniter中的routes.php是什么

[英]What is routes.php in codeigniter

what is definitely the function of routes.php in codeigniter ? codeigniterroutes.php的功能是什么?

My teacher was taught about it and said your views won't work if you don't use routes such as example: 我的老师接受过关于它的教导,并说如果你不使用例如以下的路线你的观点将不起作用:

$route['blabla'] = ['blabla/blabla']; and everything about it! 以及它的一切!

But for me, it's working without using any routes. 但对我来说,它没有使用任何路线。 I don't have idea why we should using routes. 我不知道为什么要使用路线。 It's only waste time. 这只是浪费时间。 Can anyone explain? 谁能解释一下?

In codeigniter, PHP files are served in a different way rather than accessing the PHP files directly from the browser. 在codeigniter中,PHP文件以不同的方式提供,而不是直接从浏览器访问PHP文件。 This process is called as Routing . 此过程称为路由

Our code will work without rewriting the url in routes.php. 我们的代码无需重写 routes.php中的url即可运行。

We all want to show our web page in a more convenient way, so that it can make more sense to visitors(also to search engines, of course). 我们都希望以更方便的方式显示我们的网页,以便它对访问者(当然也包括搜索引擎)更有意义。 Such as, one should understand in brief what contents a page contains, just by checking out the URL in the browser address bar. 例如,只需通过检查浏览器地址栏中的URL,即可简要了解页面包含的内容。 If we keep this as it is in a way what can be understood by server scripts(PHP,Asp.NET etc), like as follows, shouldn't fulfill our goal much: 如果我们保持这一点,就像服务器脚本(PHP,Asp.NET等)可以理解的那样,如下所示,不应该实现我们的目标:

http://yourdomain.com?p=1
http://yourdomain.com?p=2
http://yourdomain.com?w=30
http://yourdomain.com?z=234
//etc......

From the above links, there is no way to understand what those pages are about. 从上面的链接中,无法理解这些页面的内容。 Now, what if it was something like as follows: 现在,如果它如下所示:

http://codesamplez.com/database/codeigniter-activerecord
http://codesamplez.com/programming/regular-expressions-in-php

These are much more meaningful, we can have a brief idea from just seeing the URL. 这些更有意义,我们可以从看到URL中得到一个简单的想法。 Also, search engines gives some more value compared to the earlier urls, that's why they are known as 'search engine friendly URL'. 此外,与早期的网址相比,搜索引擎提供了更多的价值,这就是为什么它们被称为“搜索引擎友好网址”。 So, whatever reason you choose, it's always better to use SEO friendly URL always. 所以,无论你选择什么原因,总是更好地使用SEO友好的URL。 Ok, now, we have decided to make our site more SE/visitor friendly and want to use these urls. 好的,现在,我们决定让我们的网站更加SE /访客友好,并希望使用这些网址。 Now, how we should develop our application to map this urls to original request handler scripts? 现在,我们应该如何开发应用程序以将此URL映射到原始请求处理程序脚本?

Url Rewriting/Routing actually is the technique which converts this seo friendly urls to a format that server code can understand easily/drives a request to their corresponding request handler scripts. Url Rewriting / Routing实际上是将这个seo友好URL转换为服务器代码可以轻松理解的格式/将请求驱动到相应的请求处理程序脚本的技术。

CodeIgniter has user-friendly URI routing system, so that you can easily re-route URL. CodeIgniter具有用户友好的URI路由系统,因此您可以轻松地重新路由URL。 Typically, there is a one-to-one relationship between a URL string and its corresponding controller class/method. 通常,URL字符串与其对应的控制器类/方法之间存在一对一的关系。 The segments in a URI normally follow this pattern − URI中的段通常遵循这种模式 -

your-domain.com/class/method/id/
  • The first segment represents the controller class that should be invoked. 一个段表示应该调用的控制器类。
  • The second segment represents the class function, or method, that should be called. 第二个段表示应该调用的类函数或方法。
  • The third , and any additional segments, represent the ID and any variables that will be passed to the controller. 第三个和任何其他段表示ID和将传递给控制器​​的任何变量。

In some situations, you may want to change this default routing mechanism. 在某些情况下,您可能希望更改此默认路由机制。 CodeIgniter provides facility through which you can set your own routing rules. CodeIgniter提供了一些工具,您可以通过它来设置自己的路由规则。

There is a particular file where you can handle all these. 有一个特定的文件,您可以处理所有这些。 The file is located at application/config/routes.php(you already knew that). 该文件位于application / config / routes.php(您已经知道)。 You will find an array called $route in which you can customize your routing rules. 您将找到一个名为$ route的数组,您可以在其中自定义路由规则。 The key in the $route array will decide what to route and the value will decide where to route. $ route数组中的键将决定路由的内容,值将决定路由的位置。 There are three reserved routes in CodeIgniter. CodeIgniter中有三条保留路由。

$route['default_controller'] $路线[ 'default_controller']

This route indicates which controller class should be loaded, if the URI contains no data, which will be the case when people load your root URL. 如果URI不包含数据,则此路由指示应加载哪个控制器类,这是人们加载根URL时的情况。 You are encouraged to have a default route otherwise a 404 page will appear, by default. 建议您使用默认路由,否则将显示404页面。 We can set home page of website here so it will be loaded by default. 我们可以在这里设置网站的主页,以便默认加载。

$route['404_override'] $路线[ '404_override']

This route indicates which controller class should be loaded if the requested controller is not found. 此路由指示如果找不到请求的控制器,应加载哪个控制器类。 It will override the default 404 error page. 它将覆盖默认的404错误页面。 It won't affect to the show_404() function, which will continue loading the default error_404.php file in application/views/errors/error_404.php. 它不会影响the show_404()函数,它将继续加载application/views/errors/error_404.php.的默认error_404.php文件application/views/errors/error_404.php.

$route['translate_uri_dashes'] $路线[ 'translate_uri_dashes']

As evident by the Boolean value, this is not exactly a route. 布尔值显而易见,这不是一条路线。 This option enables you to automatically replace dashes ('-') with underscores in the controller and method URI segments, thus saving you additional route entries if you need to do that. 此选项使您可以使用控制器和方法URI段中的下划线自动替换短划线(' - '),从而在需要时节省其他路径条目。 This is required because the dash is not a valid class or method-name character and will cause a fatal error, if you try to use it. 这是必需的,因为破折号不是有效的类或方法名称字符,如果您尝试使用它,将导致致命错误。

Watch this video for a basic idea 观看此视频,了解基本概念

Refer this documentation too 请参阅此文档

In CI code will run without routes.php. 在CI代码中将运行没有routes.php。 routes.php are use to customise the url/routes like if you have url like routes.php用于自定义url / routes,就像你有url一样

example.com/pages/about_us

where "pages" is your controller and "about_us" is method in that controller, if you want to show url without controller like example.com/about_us , then you need to use routes.php in this way 其中“pages”是你的控制器,“about_us”是该控制器中的方法,如果你想显示没有像example.com/about_us这样的控制器的url,那么你需要以这种方式使用routes.php

$route['about_us'] = ['pages/about_us'];

Routing in codeigniter one of major feature which is used for user friendly URL and SEO Friendly google search. 在codeigniter中路由一个主要功能,用于用户友好的URL和SEO友好的谷歌搜索。

What does it mean: if any user searched for any product in your website so the URL will be www.domain.in/categalog/Deskstop-Intel-124343 Now in this case the url is user friendly how to interpret it is maintained in routing. 这是什么意思:如果有任何用户搜索了您网站上的任何产品,那么URL将是www.domain.in/categalog/Deskstop-Intel-124343现在在这种情况下,网址是用户友好的如何解释它在路由中维护。

Every time the url is hitted control flow to routes and from routes To controller 每次url被命中控制流到routes和从routescontroller

$route['product/(:any)'] = 'catalog/product_lookup';

Now categlog/product_lookup is interpreted as product/any means there is one controller Product which accept Id/text as input. 现在,categorieslog / product_lookup被解释为产品/任何意味着有一个控制器Product接受Id /文本作为输入。

This is as simple as making the user defined url as per our requirement. 这就像根据我们的要求制作用户定义的URL一样简单。

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

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