Asked  7 Months ago    Answers:  5   Viewed   32 times

I'm working on a web page project. I decided to use Apache, PHP (5.1.7, version imposed by my service provider) and Dwoo (templating) for this purpose.

I want to route URLs to my templates. I'm aware there are many frameworks doing this kind of thing. I'm just wondering if there's a nice way to achieve it without.

I've set up my project as follows:

  • src/dwoo - Dwoo files
  • index.php - This should handle routing. Currently it just renders the front page of the site using a template.
  • templates - Templates that represent actual pages.

There is minimal amount of business logic (no real model). It's all just pretty static pages. Using templates makes maintenance work easier (inheritance ie.).

Any idea how to set up routing in this case? I guess ideally each given URL should route via index.php that then somehow decides which template to render (ie. /category/pagename would map to templates/category/pagename.tpl).

 Answers

68

Use mod_rewrite to route everything to a single index.php file. Then check the variable in $_SERVER['REQUEST_URI'] within this file to dispatch to the required handler.

This configuration will enable mod_rewrite, if it's installed:

DirectorySlash Off
Options FollowSymLinks Indexes
DirectoryIndex index.php

RewriteEngine on

RewriteCond %{REQUEST_FILENAME}  -d
RewriteRule  ^.*$  -  [L]

RewriteCond %{REQUEST_FILENAME}  -f
RewriteRule  ^.*$  -  [L]

RewriteRule ^.*$    index.php [L]
Wednesday, March 31, 2021
 
Student
answered 7 Months ago
100

The pattern that most MVC frameworks use is a front controller that calls on a router. The front controller is typically an index.php in your web root. Next, all requests that aren't for existing files (like js, css, and image assets) need to be sent to this controller. In apache, you can do this with mod_rewrite:

RewriteRule ^index.php$ - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L,QSA]

However, the recommended way in Apache 2.5 is with FallbackResource:

FallbackResource index.php

IIS has similar functionality if that's what you're using.

In index.php, you can access the URL originally requested with $_SERVER['REQUEST_URI']. You should include your router (which should be outside of the web root) and invoke it with the request URI. Example:

require '../router.php';

$router = new Router();
$router->process($_SERVER['HTTP_METHOD'], $_SERVER['REQUEST_URI'], $_GET, $_POST);

Then your router can find the appropriate controller to route the request to. Read more on the MVC framework, and study some examples to better understand how others have implemented it.

Wednesday, March 31, 2021
 
e_i_pi
answered 7 Months ago
59

I ran into the same issue and found the following solution in the documentation: http://symfony.com/doc/current/book/testing.html#your-first-functional-test

To run your functional tests, the WebTestCase class bootstraps the kernel of your application. In most cases, this happens automatically. However, if your kernel is in a non-standard directory, you'll need to modify your phpunit.xml.dist file to set the KERNEL_DIR environment variable to the directory of your kernel:

<phpunit>
    <!-- ... -->
    <php>
        <server name="KERNEL_DIR" value="/path/to/your/app/" />
    </php>
    <!-- ... -->
</phpunit>

So check your phpunit.xml.dist configuration file and try to add the absolute path to your app-directory.

Hope it helps.

Friday, July 30, 2021
 
Bálint Molnár
answered 3 Months ago
30

in htaccess file insert this code :

RewriteEngine On
RewriteRule ^admin/([^/]*)/([^/]*)$ /admin/index.php?View=$1&Model=$2 [L]

The rewritten URL:

http://mysite.com/admin/List/User

this site very useful for generate rewrite URL

http://www.generateit.net/mod-rewrite/

Saturday, July 31, 2021
 
Pascal
answered 3 Months ago
88

Silly mistake on my part... simply forgot to add phpunit as a dependency in the project. For anyone else that gets this error, to composer.json add:

"require-dev": {
    "phpunit/phpunit": "3.7.*"
},

And then run:

composer update

That solved the problem.

Friday, September 17, 2021
 
Samir Sabri
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :