1

I am using Yii2 framework for my PHP development. In my view files, If I want to call any of the functions, I just use Class/Function name

Eg : www.example.com/SiteController[class name]/index[function name]

And it is calling the function. I like to know, How to do the same in a pure php script ?. I searched in many places and I could get the suggestions for special_autoload_register();. But I could not understand the exact practical application. Guidance is expected and Thanks is advance.

Igbanam
  • 5,904
  • 5
  • 44
  • 68

2 Answers2

2

Its easy :).

It's all based on apache module mod_rewrite. This module allows you to modify path behavior in .htaccess file.

Using this .htaccess file

RewriteEngine On

# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

# The following rewrites all other queries to index.php. The
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

you will get following behavior:

  1. If requested file exists (/styles/style.css - styles, javascripts, images, etc) serve it (dont change anything on current behavior)
  2. If requested file doesn't exist, go to index.php.

If you are redirected to index.php, you can find full requested url in $_SERVER['REDIRECT_URL'] or $_SERVER['REQUEST_URI']. There you can take it, parse and based on requested uri, behave.

Please take this as explanation post, not a guide on how to get this going. Mostly it requires some apache configuration because mod_rewrite is mostly disabled by default.

If you want to get things going, I would recommend this post.

To fully answer your question, you can for example explode request uri by "/" sign, save first part into $firstPart and second into $secondPart and then have

$controllerName = $firstPart."Controller";
$controller = new $controllerName;
$actionName = $secondPart."Action"
$response = $controller->$actionName();

So if you call /help/me, helpController->meAction() will be called.

I hope I helped :)

Wax Cage
  • 788
  • 2
  • 8
  • 24
1

To do this, Yii 2 (and other PHP frameworks) have routers. The router in Yii 2 is the UrlManager class.

I would not advice you write a router from scratch for a solution you want to deploy. There are routing packages in PHP which you could easily use in your solution. I like Klein. It's a pure router in PHP.

However, if for academic purposes, you want to know how routing works in PHP, get to understand the $_SERVER reserved variable. It has all the details of the HTTP request coming to your script. You can then use the details from this to call the specific function you want to call.

Igbanam
  • 5,904
  • 5
  • 44
  • 68