Apache routing to index confusion


#1

Hello PHP World, I am a beginner back-end programmer. I have very limited knowledge and experience. However, i should still be able to make a website if i want to do so. I often feel that tech giants, or powers that be, try to control us with learning curves. I can design an html and css website but i need backend power to create a viable business, albeit small business. My lack of knowledge keeps getting in the way. I often receive tips to use frameworks which i find to be cumbersome and more like inhibitors to my html css designs. Everytime i read about how to use a framework, i leave the page in seconds. I do not want to attend a university to build a PHP login website. My point here is that i am trying to learn about routing, controllers and frameworks on my own. Today, i have tried to think about this concept and try some things to see what works. I discovered something that is very problematic and i seek an answer to this problem:

i have enabled the Apache Rewrite engine and set conditions as follows:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

I understand this to mean “if file or directory does not exist, then send to index.php” and this is how it works. Originally, i thought that all traffic is routed to index.php but the not symbol is pretty clear now.

Meantime, i wanted to see how one could capture a fake link. Thus, i came up with the following code (this is just test code to see what happens). So index.php:

<?php
 $street = $_SERVER['REQUEST_URI'];
 $streetMap = explode("/", parse_url($street, PHP_URL_PATH));
 $street = NULL;

 if ($streetMap[1] == "OneStr") {
      echo "Street One<br />";
      echo "<a href=\"/\">Return to index</a>";
      exit;
 } else {
?>

<html>
<head>
  <title></title>
</head>
<body>

<a href="OneStr/">Street One</a><br />

</body>
</html>

<?php
 }
 exit;
?>

if i exclude the if else block, then every time the OneStr link is clicked, the fake directory name is added to the address bar. Eventually, Apache throws an error because the string has exceeded a limit. When i add a handler for the fake directory, the fake directory no longer gets appended to the address each time it is clicked. Is Apache still cycling this fake dir? so, even with the fakedir handler, Apache will still crash when it reaches the limit? Then, how does a router/controller work? what am i not understanding? or is this correct?

Also, i have no idea why we all cannot build an easier template to replace frameworks. I think of a website like this:

website=town, folders=streets, pages=addresses/buildings/houses, session=vehicle/auto, user=driver

all autos have a vin number, a tag, a tag expiration, must have a valid registration, some have inspection stickers etc.

all drivers must have a driver license, a registration, insurance, etc

police cruise the town looking for traffic violations etc and can call dispatch (database) for suspect details etc.

i hope to start a github where programmers can make this happen. Web design should be easier and faster and still secure.

Anyway, am i on the right path to understanding routers or not?


#2

I think you’re just messing up the routes by linking to a sub folder. Try changing your links to go from the root instead.

<a href="/OneStr">onestr</a>

To be honest your route is more similar to the university way of learning coding, where you build every component yourself. This is great for learning, but horrible for production and value. A client will very rarely be interested in paying you to write what an existing solution (ie wordpress) or a framework (ie laravel) already offers. When you add that the code in popular frameworks/routers/template engines/etc are vetted by a lot of great coders you quickly realize that the bar is set quite high on a custom built component to even be on par with the existing ones.

While it is tempting to call these frameworks/components free code please be aware they in no way come without you needing to dedicate some time to learn how to use them. More often than you would like you will also hit walls using other people’s code that you may not have hit if writing all yourself.

I still believe using popular 3rd party code is superior to writing everything in house. The time needed to play around with them and some times wrestle them into submission or work around them is well worth it.

Integral parts of your app like auth, routing, notifications, mailing, etc is handled by code written in collaboration between a lot of people who really know what they are doing.

Your code will be much easier to adopt by other coders, as they have probably worked with a lot of it already (through using the same components).

You get free testing. Most (popular at least) 3rd party code include a test suite that you’d otherwise have to write yourself. Because we all write tests, right? ^^

I haven’t got a CS degree or anything to show off other than 20 years of fiddling with web development on my own (+ a few years as a professional web dev).

I’d strongly suggest not reading about frameworks, but trying one. It should take just a couple of commands to get Symfony running and then you have a foundation in place to build an app. You may quickly see that this allows you to just focus on your apps code, instead of all the boilerplate around it.

Now if you’re very new to backend coding then jumping into a full fledged application or a professional framework isn’t easy. Coding is often misunderstood as being really easy to get into, but if you want to do things properly (which it really seems like from your posts) then expect to sink quite a few (a lot) hours into this before feeling comfortable


#3

Excellent post, Sir Jim. You are correct about the uri. I see that i used a sub.

I have downloaded a microframework called fat-free. Is this ok?
I will also fetch Symfony and see what happens.

I am really just messing with code to try to better understand the frameworks. I have a strange way of learning. I sometimes have to understand why something works before i can fully understand what it is and how to use it.

Well you’re an expert in my opinion. I have learned that experience is much better than theory in most cases. I will bet that you code better than alot of university grads…