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…

#4

I woud also suggest working through the shortest “no-framework framework” tutorial you can find for php to get a better idea of what the main components are. (but they usually don’t cover auth, notifications, and mail)

#5

good suggestion, thinsoldier. I’ve allready found a few sites with no-framework framework data.

one thing that i always forget to mention is that i am not a programmer and i am not delusional about this matter. I’ve always considered hacking (a term for my untrained, unskilled, messy attempt at programming) to be hobby. Albeit, a hobby with benefits. Most people want to learn programming to land a job or to create a service/software. I have never set out to be a programming employee. I understand, fully, why the business world demands frameworks and framework trained programmers. Sloppy code with too many programmers led to sql injections and xss attacks. Stupidity led to unhashed db passwords. Anyway, i am just trying to find a way to profit from my photographs, scientific data and research via a subscription-based website. For someone like me, a framework is overkill. I always say that i shouldn’t implement code that i don’t understand (in support of a framework), yet, i don’t understand frameworks either. Thus, i shouldn’t use a framework which is coded by people with knowledge well beyond my own. I like to study the frameworks to see how they work. Maybe i can learn enough to avoid the include/require spaghetti code method of “legacy” app design. However, i see similarities between include/require and replacing include/require with routers/controllers/classes that essentially include/require based upon many more application switches and controll mechanisms. Still, i fully understand the use of frameworks but i still do not think that a framework is best for my small, isolated, independent hobby-based attempt to build an online presence. I do wish to be a better programmer and i will take time to learn this framework technology.

sorry for typing so much. Anyway, thank you for the tip, thinsoldier. I will definitely read these no-framework framework documents.

#6

I would say the complete opposite then. At the very least learn from the no-framework framework tutorials and roll your own bare bones framework comprised of componenets written by people who knew what they were doing on each component. I worked full time as a php programmer for a dozen years without frameworks and even without using other people’s individual components. It was very bad. Security and especially productivity suffered greatly.

Shortly after leaving my old job I built a small site for a company to keep track of items ordered and shipped from other countries through various shipping services. It needed database models, routing, auth, e-mail, html templating for the views, and a few other things. I used 3rd party packes for the whole thing and it was 50x faster and easier than anything I had done at my old job. The only problem was I had to concoct my own controller class to make it work with the router package, view package, and model package. If I had just used Laravel or even CodeIgniter it would have been even faster and easier. Even though CodeIgniter is old as dirt it is still probably a hell of a lot more secure and maintainable than anything I ever wrote from scratch at my old job.