Php Routing Problems

Source: Internet
Author: User
PHP files can be require to each other. But if the nested directory is deep, is there any good solution? For example: {code ...} in such a directory, if you need to introduce hook in test2, It is very troublesome to find the directory. It can be done if the cross-file global variable is used ,... PHP files can be require to each other. But if the nested directory is deep, is there any good solution?

For example:

/--root|_lib|   |_hook.php|_src|_view|   |_page|   |_js|   |_img|_handler    |_test1        |_test2            |_test2.php

In such a directory, if you need to introduce hook in test2, It is very troublesome to find the directory. You can do this if there are too many files, cross-global variables must be used in each file, which may be messy. Can I define the directory structure in a single file and provide it to other files? How can we implement this change?

Thank you for your answers!

Reply content:

PHP files can be require to each other. But if the nested directory is deep, is there any good solution?

For example:

/--root|_lib|   |_hook.php|_src|_view|   |_page|   |_js|   |_img|_handler    |_test1        |_test2            |_test2.php

In such a directory, if you need to introduce hook in test2, It is very troublesome to find the directory. You can do this if there are too many files, cross-global variables must be used in each file, which may be messy. Can I define the directory structure in a single file and provide it to other files? How can we implement this change?

Thank you for your answers!

Http://www.php-fig.org/psr/psr-0/

I immediately thought of the underline name.

new Path1_Path2_Path3()

Equal to Path1/Path2/Path3.php
Include require.

Thinkphp has a namespace usage. For more information, see

You can write a function by yourself. The function is used to load the required files. When you need to introduce a file, traverse the directory to find the file. However, the efficiency is relatively low.

We recommend that you use constants or namespaces)

We recommend that you use a streamlined Router database as the routing controller handler and hook. You can use hook to easily customize parameter filtering and login check.

(new Router())->error(405, function($message){    header('Location: /hello/world', true, 302);})->get('/hello/:name', function($name){    echo "Hello $name !!!";})->execute();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.