Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create pages from databse Using php [Like what framework or cms are doing ]

Hi i have a database table and a php form in which i can insert table entry . For example i have a form in which i can add post title and post content . So in the table i have 4 column (1)post_id, (2)post_title, (3)post_content, (4)post_url. So currently i fill the form with title="red color" content="red color is so bright and attractive ". so it will store on database.

Now what i need is when there is an entry in table i have to generate page url also . Here it is example.com/red-color. And some on can take example.com/red-color i need to display the content , and google boot need to automatically fetch through page .

I know this is some complicated process like what wordpress is doing when user insert the content to the post then it will create new page with unique url .

I need only some basic ideas for to proceed .

If anyone know please share the knowledge .

like image 538
Abilash Erikson Avatar asked Apr 21 '26 11:04

Abilash Erikson


1 Answers

First: you need to have mechanism to redirect all you requests to your router. In case of Apache you can add .htaccess file with next content:

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

In case of Nginx (part of sample config, the main logic is in @rewrite stream and try_files directive)

server {
    ....

    location / {
        index index.html index.php;
        try_files $uri $uri/ @rewrite;
    }

    location @rewrite {
        rewrite / /index.php;
    }

    ## Execute PHP scripts
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

in index.php file you will have access to Path which was originally requested $_SERVER['REDIRECT_URL']

and here(in index.php file): (if that variable set) try to lookup value of $_SERVER['REDIRECT_URL'] in your table, in case if you find that: display title, content and so on. In case if you can not find it - you should dispaly 404 page (with information that post wasn't found)

In simpliest approach - that is.

Good luck :)

like image 77
Alex Kapustin Avatar answered Apr 24 '26 00:04

Alex Kapustin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!