Custom CMS, pretty permalinks like commercial CMS

I am currently whipping a very simple CMS for a personal project. This is both for my own education, and for everything. One of the questions I have is how can I reach url / permalinks without file extensions. I understand using get variables to retrieve data from a database, but how do you convert it to something like www.url.com/posttitle, and not something like www.url.com/?posttitle='blablabla.

Also on a slightly different topic, can I point out that EASY uses the framework to develop sites that deal with memberships and membership lists, for example, Craigslist.

I am currently developing in Wordpress and am quite capable, but less familiar with OOPHP and CMS custom development from a basic level.

Thanks in advance for any help or suggestions.

+4
source share
3 answers

You should use the .htaccess file to send all requests to your front controller (usually just an index.php script), and then the script matches the incoming write request in your database.

For example, if you have a database table named pages with four columns: id , title , slug and content , the following simple implementation ...

 RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.+)$ index.php/$1 [NC,L] 

This tells Apache to accept all requests that are not a file or destination, and send them to index.php .

Your index.php might look like this:

 <?php // Take request URI // Would be something like 'your-slug' $request = trim($_SERVER['REQUEST_URI'], '/'); // Set up database connection and attempt to match slug $sql = "SELECT * FROM pages WHERE slug = ? LIMIT 1"; $smt = $db->prepare($sql); $smt->execute(array($request)); $page = $smt->fetchObject(); if (! $page) { // Page was not found matching slug header('HTTP/1.1 404 Not Found'); exit; } // Display matching page in a template 

From here you can build on it.

+9
source

This can be done simply by rewriting the URL (in .htaccess placed in the root structure of your site structure).

Or you can rewrite everything to your index.php and then parse it here.

You just grab the URI part of the URL from the variable $ _ SERVER (look at QUERY_STRING or just var_dump($_SERVER) to see which key contains what).

Here is an example .htaccess file for overwriting everything:

 RewriteEngine on # rewrite everything except for assets to index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule !\.(js|ico|gif|jpg|png|css)$ /index.php [L] 
+2
source

File-free extensions are usually the result of an abstraction of the URL path - this means that your application should interpret everything after the URL and start presenting data based on this.

Consider the following:

http://www.url.com/about-us

In a normal HTTP request (for example, Apache), Apache will try to serve the shared folder named /about-us , and since it is not specified, the static file index.php .

With any of the popular MVC frameworks such as CodeIgniter, CakePHP, Ruby on Rails, etc., /about-us maps to what is called a route that loads the assets related to this page. Therefore, instead of loading a static page, it ends in the database, captures data for this page, captures a template, and dynamically serves the file. This, in fact, is one way to get "pretty" URLs.

If you want to knock over your own, I highly recommend any of the above infrastructures. Don't just use them without understanding them, try to understand what the execution process is. Get to know exactly what each request does.

As for authentication, I know that there are several options in Rails, such as Devise and CanCan. These are mostly pre-coded authentication modules that allow you to easily configure them.

+2
source

Source: https://habr.com/ru/post/1385664/


All Articles