Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to load classes based on pretty URLs in MVC-like page?
    primarykey
    data
    text
    <p>I would like to ask for some tips, on how solve this problem. I'm trying to build my own MVC website. I learned the basics of the URL.</p> <pre><code>http://example.com/blog/cosplay/cosplayer-expo-today </code></pre> <p>blog -> the controller<br/>cosplay -> the method in controller<br/>cosplayer-expo-today ->variable in method</p> <p>What if i dynamically extend the category in my blog controller? Will I need to create the method, or is there some trick to do that automatically? I mean... i have these categories now: cosplay,game,movie,series. So I need to create these methods in controller, but they all do the same thing, namely select other category from database.</p> <ul> <li>function cosplay() = example.com/blog/cosplay/</li> <li>function game() = example.com/blog/game/</li> <li>function movie() = example.com/blog/movie/</li> <li>function series() = example.com/blog/series/</li> </ul> <p>Is there any good advice on how can i write my controller to do that automatically? I mean if I upload a new category in my database, but i don't want to modify the controller. Is it possible? Thanks for the help!</p> <p>UPDATE</p> <p>Here is my URL exploder class</p> <pre><code>class Autoload { var $url; var $controller; function __construct() { $this-&gt;url = $_GET['url']; //HNEM ÜRES AZ URL if($this-&gt;url!='' &amp;&amp; !empty($this-&gt;url)) { require 'application/config/routes.php'; //URL VIZSGÁLATA $this-&gt;rewrite_url($this-&gt;url); //URL SZÉTBONTÁSA $this-&gt;url = explode('/', $this-&gt;url); $file = 'application/controllers/'.$this-&gt;url[0].'.php'; //LÉTEZIK A CONTROLLER? if(file_exists($file)) { require $file; $this-&gt;controller = new $this-&gt;url[0]; //KÉRELEM ALATT VAN AZ ALOLDAL? if(isset($this-&gt;url[1])) { //LÉTEZIK A METÓDUS? ENGEDÉLYEZVE VAN? if(method_exists($this-&gt;controller, $this-&gt;url[1]) &amp;&amp; in_array($this-&gt;url[1], $route[$this-&gt;url[0]])) { if(isset($this-&gt;url[2])) { $this-&gt;controller-&gt;{$this-&gt;url[1]}($this-&gt;url[2]); } else { $this-&gt;controller-&gt;{$this-&gt;url[1]}(); } } else { header('location:'.SITE.$this-&gt;url[0]); die(); } } } else { header('location:'.SITE); die(); } } else { header('location:'.SITE.'blog'); die(); } } /** * Első lépésben megvizsgáljuk, hogy a kapott szöveg tartalmaz-e nagybetűt. Amennyiben igen átalakítjuk kisbetűsre.&lt;br/&gt; * Második lépésben megnézzük, hogy a kapott szöveg '/'-re végződik-e. Amennyiben igen levágjuk azt.&lt;br/&gt; * Harmadik lépésben újra töltjük az oldalt a formázott szöveggel. * * @param string $url Korábban beolvasott URL. */ private function rewrite_url($url) { //HA NAGYBETŰ VAN AZ URL-BEN VAGY '/'-RE VÉGZŐDIK if(preg_match('/[A-Z]/', $url) || substr($url, -1)=='/') { //NAGYBETŰS AZ URL KICSIRE ALAKÍTJUK if(preg_match('/[A-Z]/', $url)) { $url = strtolower($url); } //HA '/'-RE VÉGZŐDIK LEVÁGJUK if(substr($url, -1)=='/') { $url = substr($url, 0, strlen($url)-1); } header('location:'.SITE.$url); die(); } } } </code></pre> <p>And here is my .htacces</p> <pre><code>Options +FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.+)$ index.php?url=$1 [QSA,L] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload