0

I'm currently introduce myself to the world of WebDev and trying to create a very basic mvc framework (like most I guess lol) for learning purposes.

Now I want to redirect all requests to /bootstrap.php (which handles routing, invokes controller, init smarty etc) from within my /.htaccess file. My router class needs requests like the following examples (changeable if needed):

https://www.stuck1a.de/home?foo=bar      // ~> /mvc/views/pages/home/index.tpl?foo=bar
https://www.stuck1a.de/home/index?x=y    // ~> /mvc/views/pages/home/index.tpl?x=y
https://www.stuck1a.de/music/byartist    // ~> /mvc/views/pages/music/byartist.tpl
...

My problem now is, that the path /home/* technically doesn't exist (SEO), so a request like the examples above lead to a 404 (of course) instead of make use of redirection rules from /.htaccess.

Well, I think creating a file /home/.htaccess which redirects requests to /bootstrap.php would solve this, but that can't be the whole point of the matter...

Because SEO-URL's are very common, I'm pretty sure, there must be an easy solution for my problem, that I just can't think of. I hope you understand what I mean. If you need more informations or want to see sources, just let me know.

Thanks and best regards,

Stuck1A

stuck1a
  • 13
  • 6
  • Damn, I didn't even think about that .htaccess files will work recursively. So the following will do the job: RewriteRule ^(.*)$ bootstrap.php [NC,L,QSA] – stuck1a Oct 03 '21 at 09:26
  • Thanks for the Link. I guess i already understood the general function of controllers in a mvc pattern. But since I'm very new to web development I mostly have trouble with the implementation. But my very basic mvc framework now works after solve the redirection issues. Well I already stuck in next problem *gg* (create controller instances instead of simply include them, but my router can't access the controller as class). But with enough patience and perseverance, I will solve this problem as well :) – stuck1a Oct 03 '21 at 10:38
  • Hi. The router doesn't have the responsibility of dispatching a certain controller. This task is performed by a route dispatcher. The router's responsibility is only to match the user request components (the HTTP method and the URI path of the browser's URL) to the components of each route (the list of allowed HTTP methods and the pattern) in a predefined collection of routes. – PajuranCodes Oct 03 '21 at 17:51
  • If the router finds a matching route, it just returns it (as an instance, or as an array composed of its components: controller name, parameter values, route name, etc). The route dispatcher will further receive the infos and will dispatch the given controller, passing it the route parameters. – PajuranCodes Oct 03 '21 at 17:51
  • @dakis. Is there any benefit from separating router and dispatcher except following conventions and the logical seperation? I saw a lot examples which followed your proposal but also alot which combined dispatching & routing in one script. – stuck1a Oct 05 '21 at 17:25
  • Well, my router doesn't use predefined routing maps for now (except default/fallback routes). It decides which controller to dispatch only by the request + naming convetions, since I preceive the higher dynamic (no need to add routes to a map). But I am happy to be taught better – stuck1a Oct 05 '21 at 17:32
  • This would actually be a great question for SO. From my point of view, the separation of the router from the route dispatcher allows respecting the [Single Responsibility Principle](https://en.wikipedia.org/wiki/Single-responsibility_principle). Router: match the request to the route collection, handle all exception/error cases, return route. Dispatcher: check the type of the handler of the found route (either a custom associative array with callables of type string|array|object as element values, or just a callable of type string|array|object), invoke it, handle the raised exceptions/errors. – PajuranCodes Oct 05 '21 at 20:01
  • Your route dispatcher could, for example, use [PHP-DI Invoker](https://github.com/PHP-DI/Invoker) to invoke the controller/view method(s) - a great package. Also, personally, I would recommend you to already include a RegEx-based router in your project(s), like [FastRoute](https://github.com/nikic/FastRoute). Its advantage: flexibility. Since, sooner or later, you will discover, that your conventions-based router will limit your vision more and more. – PajuranCodes Oct 05 '21 at 21:39
  • In general, developing your own web framework doesn't mean to develop each needed component by yourself (a router, a [DI container](https://php-di.org/), a [PSR-7 implementation](https://docs.laminas.dev/laminas-diactoros/v2/overview/), a [template engine](https://twig.symfony.com/), etc), even for learning purposes. Nowadays, these components are already developed, good documented and posted online by various teams as open source projects. Therefore it is more about understanding what each component can do and how to include it in your framework in such a way, – PajuranCodes Oct 05 '21 at 21:40
  • that every future MVC-based application of yours can use it flawlessly. It is also about understanding which components NOT to use at all, even though a lot of web users recommend using them. Examples: [static methods](https://www.php.net/manual/en/language.oop5.static.php), [singleton pattern](https://en.wikipedia.org/wiki/Singleton_pattern) (for db connections, at least), [ORM](https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping) software, [active record](https://www.martinfowler.com/eaaCatalog/activeRecord.html) pattern, database wrapper classes, – PajuranCodes Oct 05 '21 at 21:47
  • even query builders (like [this](https://www.yiiframework.com/doc/guide/2.0/en/db-query-builder))... PS: There is no such term as _"MVC framework"_. Instead, there is a _"web MVC-based application"_ (a web application developed using the MVC pattern), which uses a _"web framework"_. – PajuranCodes Oct 05 '21 at 22:08
  • Thanks for this many advises. I've watched and tried alot frameworks but most of them couldn't match my personal needings as good as when I code it by myself. Only Smarty is so amazing I've decided to use it for templating. For the rest I've adapted be the ideas as seen in laminas, oxid and countless other frameworks. In general, I am quite critical of the heavy use of frameworks, because I find it regrettable that the former art of programming in the profession has been reduced to connecting packages with one another :/ – stuck1a Oct 05 '21 at 23:09
  • My target for this project is to create something similar minimalistic like divix1988/laminas-smarty, for later use in small website projects (homepages etc). Splitting into responsibilities sounds good, guess I will adapt this principle – stuck1a Oct 05 '21 at 23:13
  • You are welcome. The best thing is to develop your own framework and to also use it in your applications. In the last two years, most frameworks radically changed their structure in a good direction: they are developing now independent components/libraries, instead of bundling all their functionality into a massive codebase, to which your project would be completely dependendent. I worked with Smarty. A mature software. Though I find Twig more elegant. – PajuranCodes Oct 06 '21 at 01:26
  • As for frameworks, in general and, especially, for learning purposes, I can only recommend [Laminas Components](https://docs.laminas.dev/components/), [Laminas Mezzio](https://docs.mezzio.dev/), [Symfony](https://symfony.com/doc/current/index.html) and, still with some reserves, [Slim 4](https://www.slimframework.com/docs/v4/). I wish you best of luck and have fun with your projects. – PajuranCodes Oct 06 '21 at 01:27

0 Answers0