Copy Source | Copy HTML < IfModule mod_rewrite . c > RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [QSA,L] </ IfModule >
Copy Source | Copy HTML < IfModule mod_rewrite . c > RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [QSA,L] </ IfModule >
Copy Source | Copy HTML < IfModule mod_rewrite . c > RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [QSA,L] </ IfModule >
Copy Source | Copy HTML < IfModule mod_rewrite . c > RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [QSA,L] </ IfModule >
Copy Source | Copy HTML < IfModule mod_rewrite . c > RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [QSA,L] </ IfModule >
Copy Source | Copy HTML < IfModule mod_rewrite . c > RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [QSA,L] </ IfModule >
Copy Source | Copy HTML < IfModule mod_rewrite . c > RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [QSA,L] </ IfModule >
Copy Source | Copy HTML < IfModule mod_rewrite . c > RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [QSA,L] </ IfModule >
Copy Source | Copy HTML
- function Request ( $ path , $ callback )
- {
- if ( $ path == $ _SERVER [ 'REQUEST_URI' ]) return call_user_func ( $ callback );
- }
Copy Source | Copy HTML
- function array_filter_callback_no_empty_str ( $ value )
- {
- return $ value ! = '' ;
- }
- function Request ( $ path , $ callback )
- {
- // Request variables for passing to callback
- $ args = array ();
- // Split the address to which the user has addressed (URI), into parts
- $ uri = explode ( '/' , $ _SERVER [ 'REQUEST_URI' ]);
- // Do the same with the query path.
- $ path = explode ( '/' , $ path );
- // Remove the empty parts of both arrays
- $ uri = array_values ββ(array_filter ( $ uri , array_filter_callback_no_empty_str));
- $ path = array_values ββ(array_filter ( $ path , array_filter_callback_no_empty_str));
- // If the number of parts in the URI and the path is different, exit
- if (count ( $ uri )! = count ( $ path ))
- return false ;
- // Pass through all parts of the request path
- for ( $ i = 0 ; $ i <count ( $ path ); $ i ++)
- {
- // Check if this part of the path is variable
- // Path variables are written in braces, which is what the regular expression checks.
- if (preg_match ( '| ^ \ {(. *) \} $ |' , $ path [ $ i ], $ match ))
- {
- // If so, add this variable to the array
- $ args [ $ match [ 1 ]] = $ uri [ $ i ];
- }
- else
- {
- // If the request part is not a variable, simply compare the URI with the request
- // If they do not match - exit
- if ( $ uri [ $ i ]! = $ path [ $ i ])
- return false ;
- }
- }
- // After all the checks we call callback, passing it an array with the request variables
- return call_user_func_array ( $ callback , $ args );
- }
Copy Source | Copy HTML
- function Hello ( $ who )
- {
- print "Hello, $ who" ;
- }
- Request ( '/ hello / {who}' , Hello);
Copy Source | Copy HTML
- function Request ( $ method , $ path , $ callback , $ asserts = array ())
- {
- // Check the server access method.
- // If a specific method is specified in the request and it does not match the one that was used, exit
- if ( $ method ! = '' && strtolower ( $ _SERVER [ 'REQUEST_METHOD' ])! = $ method )
- return false ;
- <...>
- // Now check if the variable is part of the path.
- if (preg_match ( '| ^ \ {(. *) \} $ |' , $ path [$ i], $ match ))
- {
- // If it is, then see if there is a regular expression to check it,
- // and if there is, then we check the corresponding part of the URI for compliance
- if (! isset ( $ asserts [ $ match [ 1 ]]) || preg_match ( $ asserts [ $ match [ 1 ]], $ uri [$ i]))
- {
- // If everything is correct, add this variable along with its value to the array
- $ args [ $ match [ 1 ]] = $ uri [$ i];
- }
- else
- {
- // If the value does not match the regular expression, exit
- return false ;
- }
- }
- <...>
- }
Copy Source | Copy HTML
- class Request
- {
- public $ method ; // Request Method (GET, POST, PUT, etc.)
- public $ path ; // Request Path
- public $ callback ; // Callback
- public $ asserts = array (); // Regular expressions for checking path variables
- // class constructor
- public function __construct ( $ method , $ path , $ callback )
- {
- $ this -> method = strtolower ( $ method );
- $ this -> path = $ path ;
- $ this -> callback = $ callback ;
- }
- // Add re regular expression to check the path variable named
- public function assert ( $ name , $ re )
- {
- $ this -> asserts [ $ name ] = $ re ;
- // Return the current instance class
- // This allows you to write similar code: $ reg-> assert ('id', '| ^ \ d + $ |') -> run ();
- return $ this ;
- }
- // Request processing function
- public function run ()
- {
- <...>
- }
- }
Copy Source | Copy HTML
- $ req = new Request ( '/ user / {id}' , UserProfile);
- $ req -> assert ( '| ^ \ d + $ |' ) -> run ();
Copy Source | Copy HTML
- function Request ( $ method , $ path , $ callback )
- {
- return new Request ( $ method , $ path , $ callback );
- }
Copy Source | Copy HTML
- Request ( '/ user / {id}' , UserProfile) -> assert ( '| ^ \ d + $ |' ) -> run ();
Copy Source | Copy HTML
- class Application
- {
- public $ requests = array ();
- /// ---
- // Implement singleton
- protected static $ instance ;
- private function __construct ()
- {
- }
- private function __clone ()
- {
- }
- public static function getInstance ()
- {
- if (! is_object (self :: $ instance ))
- {
- self :: $ instance = new self;
- }
- return self :: $ instance ;
- }
- public static function init ()
- {
- self :: getInstance ();
- }
- /// ---
- // Internal function to handle all requests
- private function i_run ()
- {
- foreach ( $ this -> requests as & $ request )
- {
- $ done = $ request -> run ( $ params );
- if ( $ done ) return true ;
- }
- return false ;
- }
- // External static wrapper function over i_run
- // Needed only for aesthetics: Application :: run () looks prettier than Application :: getInstance () -> run ()
- public static function run ()
- {
- return Application :: getInstance () -> i_run ();
- }
- }
Copy Source | Copy HTML
- class Request
- {
- <...>
- // class constructor
- public function __construct ( $ method , $ path , $ callback )
- {
- $ this -> method = strtolower ( $ method );
- $ this -> path = $ path ;
- $ this -> callback = $ callback ;
- // Add this request to the queue to the Application
- Application :: getInstance () -> requests [] = $ this ;
- }
- <...>
- public function run ()
- {
- <...>
- // After all the checks we call callback, passing it an array with the request variables
- $ result = call_user_func_array ( $ this -> callback, $ this -> args);
- // If the callback returned a boolean value, return it.
- if (is_bool ( $ result ))
- return $ result ;
- // Otherwise, return true
- else
- return true ;
- }
- }
Copy Source | Copy HTML
- new Application ();
- Request ( '/ user / {id}' , UserProfile) -> assert ( '| ^ \ d + $ |' ) -> run ();
- Application :: run ();
Copy Source | Copy HTML
- if (! class_exists ( 'Application' ))
- {
- // If the Application class is not yet declared, the script launches for the first time.
- class Request
- {
- <...>
- }
- function Request ( $ method , $ path , $ callback )
- {
- <...>
- }
- class Application
- {
- <...>
- }
- // Initialize the Application,
- Application :: init ();
- }
- else
- {
- // The script does not run for the first time.
- Application :: run ();
- }
Source: https://habr.com/ru/post/118237/
All Articles