简体   繁体   English

我不懂路由

[英]I don't understand routing

I am trying to learn a PHP framework. 我正在尝试学习一个PHP框架。 But I'm having some difficulties understanding some of the concepts of routing. 但是我在理解某些路由概念时遇到了一些困难。

I've chosen to use Flight . 我选择使用Flight Their homepage shows: 他们的主页显示:

require 'flight/Flight.php';

Flight::route('/', function(){
    echo 'hello world!';
});

Flight::start();

And I don't understand what they're using Flight::route... for. 而且我不明白他们在用Flight::route...做什么。 What am I missing? 我想念什么? This question isn't even related to Flight. 这个问题甚至与飞行无关。 It's related to just routing in general. 通常,它只与路由有关。

Routing basically maps HTTP requests to your methods/functions. 路由基本上将HTTP请求映射到您的方法/函数。

To put it simply, say you have the route: 简单来说,假设您有路线:

Flight::route('/page1', function() {
    echo 'page1!';
});

This is was basically happens: 这基本上是发生的:

  1. Client requests example.com/page1 客户要求example.com/page1
  2. Server sends query to PHP 服务器发送查询到PHP
  3. Your PHP framework parses the request URL 您的PHP框架解析请求URL
  4. Picks the correct route, in our case, page1/ 选择正确的路线,在本例中为第1 page1/
  5. And finally calls the function you passed in, so basically echo 'page1'; 最后调用您传入的函数,因此基本上echo 'page1';

Flightphp has a rather comprehensive explanation on how to set up routes here . Flightphp对于如何在此处设置航线有相当全面的解释。

You should see routes as definitions of how to handle different request patterns. 您应该将路由视为如何处理不同请求模式的定义。 The example on Flight's home page says that if you hit your site root (ie /) it will simply return "hello world!" Flight主页上的示例说,如果您打入站点根目录(即/),它将仅返回“ hello world!”。 as a response. 作为回应。

If you read further on the Flightphp install page you will notice that all requests are handled by the index.php page. 如果您在Flightphp安装页面上进一步阅读,您会注意到所有请求都由index.php页面处理。 And thus depending on the routes you define it replies with the relevant response defined for that url request pattern. 因此,根据您定义的路由,它会回复为该url请求模式定义的相关响应。

What seems to be happening in your file (I'm not familiar with Flight) 您的文件中似乎正在发生什么(我不熟悉Flight)

the require 'flight/Flight.php'; require 'flight/Flight.php'; is more than likely defining a class for all the routing. 很有可能为所有路由定义一个类。

Then Flight::route(); 然后Flight::route(); Is simply using the route() method from the class Flight without an instance of the class. 只是使用Flight类中的route()方法而没有该类的实例。

Flight::route('/', function(){
    echo 'hello world!';
});

What happens here is when a route is matched (by matched means the URI of the user matches the URI on your route, in this case www.yourdomain.com/ will match the '/' route) And then the code inside the function() callback gets executed. 这里发生的是当路由匹配时(通过匹配意味着用户的URI与您的路由上的URI匹配,在这种情况下, www.yourdomain.com/ '/'将匹配'/'路由),然后在function()内部进行编码function()回调得到执行。

If you add another route 如果您添加其他路线

Flight::route('/about', function(){
    echo 'About Us';
});

When the user visits www.yourdomain.com/about He will get whats inside that route. 当用户访问www.yourdomain.com/about他将获得该路线中的内容。

route() is a static function it seems, that means it's not specific to the object, ie you can't create an object such as route()似乎是一个静态函数,这意味着它不特定于该对象,即,您无法创建诸如

$flight = new Flight();

and then call 然后打电话

$flight->route(...)

but rather you call it via the class (not an object, which is a specific implementation of the class). 但是您可以通过类(而不是对象,它是类的特定实现)来调用它。 You call static functions of a class by using ::, in this case 您可以使用::来调用类的静态函数,在这种情况下

Flight::route(...)

The content of the route just says, when you encounter '/', do 'X'... and in your case 'X' is 路线的内容只是说,当您遇到“ /”时,请执行“ X” ...,在您的情况下,“ X”为

function(){
    echo 'hello world!';
}

in later stages you get to match stuff like 在以后的阶段中,您可以匹配诸如

'/' (homepage, i.e. "mywebsite.com/")
'/about-us' (About Us page, i.e. "mywebsite.com/about-us")
'/user/{id}' (User page, i.e. you can pass a parameter such as "mywebsite.com/user/taylor" and then get the user data)

or whatever you want. 或任何你想要的。 And instead of just writing the function into the routing file, you can tell the router to go to a specific function (usually a Controller function) and you can do more stuff there. 而且,不仅可以将功能写入路由文件中,还可以告诉路由器转到特定功能(通常是控制器功能),然后可以在其中执行更多操作。

I hope this helps! 我希望这有帮助!

Flight::route('/', function(){
    echo 'hello world!';
});

This snippet is heart of your project. 此代码片段是您项目的核心。

This will accept two parameters. 这将接受两个参数。

  1. Route 路线

  2. Method to call on this route call 呼叫此路线的方法

Consider below code snippet, If you are having your project directory http://localhost/flight_project/ , when anyone requests this directory, function defined as 'function_here' will be called. 考虑下面的代码片段,如果您的项目目录为http://localhost/flight_project/ ,则当有人请求此目录时,将调用定义为'function_here'

Flight::route('/', 'function_here');

If you have defined route like below, 如果您定义了以下路线,

Flight::route('/user/', function(){
    // do something here
});

when someone access http://localhost/flight_project/user/ , the above in-line function gets called. 当有人访问http://localhost/flight_project/user/ ,将调用上述内联函数。

More info HERE 更多信息在这里

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

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