Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

My simple PHP file-based-routing project template

NotificationsYou must be signed in to change notification settings

frozeeen/PHP-file-based-routing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This is my unstable version of file-based routing, inspired by Javascript Libraries/Frameworks, to make my project's URL tiny-little-bit-beautiful even my backend is an instant legacy code.

Sooo @frozeeen, again what's this?

Well, here you go

https://yourawesome.web/post?id=ABCDEFG12345tohttps://yourawesome.web/post/ABCDEFG12345

Installation

  1. Simply clone this project into your machine.
  2. Update the config inside theconfig/app.php.
  3. and presto! all set.

Structure

The structure is very simple

config# A folder contains some of the configurationspages# This is where your pages will livepublic# Maybe robots.txt, sitemap, some of your CSS, JS and other assetsrouter# And, this is where some tiny-little-bit magic polynomial time happens, it's the routertemplates# Collections of shared components, requiring .php just like old timesindex.php# entry or fallback php file.htaccess# We don't talk about .htaccess -Dani

How to use this?

To use this, let's head intopages folder and make some files.

Static routing

Path: pages/your-awesome-page.phpURL: http://website.com/your-awesome-page

Dynamic routing

To create a dynamic url likehttps://website.com/post?id=YOURPOSTID we're going to do it like this.

# Create a filePath: pages/post/[id].get.php# To accessURL: https://website.com/post/YOURPOSTID

and to access theid inside our code, it's just like normal$_GET, the name between the brackets[] is the parameter or key (they call thisslug).

Showing postID:<?phpecho$_GET['id'];?>

Nested Dynamic routing

We can also create a folder to become our slug.

Path:pages/post/[id]/edit.get.phpURL:pages/post/YOURPOSTID/edit

So the file structure will look like this and let's add some additional files.

/pages├── post│   └── [id]│       ├── index.get.php│       ├── index.delete.php│       ├── edit.get.php

The following endpoints correspond to the files in the structure above:

GET    /post/123DELETE /post/123GET    /post/123/edit

Request method per file

This is heavily inspired by theNitro for mapping the request method per file.

Each file is prefixed with the request method and has a .php file extension. For example, if we are creating CRUD functionality, the structure would look like this:

/[id]├── index.get.php├── index.post.php├── index.put.php├── index.delete.php

Middleware

Yet another very simple middleware.

Middleware is a script that runs before the page script is loaded; it can be used to check authentication or execute high-level intergalactic computations prior to producing the page.

To create a middleware, create a file named+middleware.php.

/pages├── auth│   ├── +middleware.php│   ├── profile.get.php│   ├── index.get.php

In the file structure above when we try to access/auth/profile or any page inside the/auth folder, it will first run the+middleware.php script.

In addition, if a given user is not authorized to visit a specific endpoint, you can use thethrow404() inside yourmiddleware.php to send a 404 error, this will return the404.php inside thepages folder.

Global Middlewares

The+middleware.global.php file is executedbefore the actual page file (index.php, etc.). If there are multiple levels of nested directories, all+middleware.global.php files in the hierarchy are applied in order, from the root to the current directory.

/pages├── +middleware.global.php       # Applies to all pages under "pages/"├── auth│   ├── +middleware.global.php   # Applies to all files under "auth/"│   ├── index.php                # Inherits "auth" middleware│   ├── nested│   │   ├── index.php            # Inherits "auth" middleware│   │   └── deep│   │       ├── index.php        # Inherits "auth" middleware├── index.php                    # Inherits global middleware from "pages/"

PS: If there are multiple levels of nested directories,all+middleware.global.php files in the hierarchy are applied in order, from the root to the current directory.

About

My simple PHP file-based-routing project template

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp