URL Friendly by title

Asked

Viewed 337 times

2

Good night people,

I am here implementing the URL friendly system on a website and I have already working for some pages, I am now locked in the ver_establishment file that and what shows the establishments in this case I have for example an establishment to list like this http://exemplo.pt/ver_estabelecimento&id=1 what I intend and replace by http://exemplo.pt/nome_do_estabelecimento where the establishment name field will come from the database.

I have the . htaccess file matched like this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?m=$1

And I have my file functions.php which is where I have the rules to embed the pages and show the content my problem is here

$url = $_GET['m'];
$urlSepara = explode('/',$url);

$primeiraUrl = $urlSepara[0];
$directorio = ("conteudos");

$permissao = array('home', 'comer', 'dormir', 'comprar', 'servicos', 'lazer', 'visitar', 'contactos', 'login', 'erro');

if(!isset($primeiraUrl) || $primeiraUrl == ''){
    include("conteudos/home.php");
}elseif(isset($primeiraUrl) && in_array($primeiraUrl, $permissao)){
    include("".$directorio."/".$primeiraUrl.".php");
}

I’d appreciate your help

  • Are the paths (parameters) already being correctly added to your database? (Low box, no accents, use of hyphen to separate words, etc)

  • yes I am no longer able to incorporate the pages now

  • I don’t know where your problem really is - whether it’s in creating rules, url’s, uploading the content of the establishment... Could you provide more details?

  • The context is very vague. Describe better, put more details of where your difficulty is.

  • I’ll explain better what the problem is

  • You should use your htaccess file to create URL rewriting rules.

  • How do I want the url name to come from the database not to do from the.php function file with htaccess just like this one ?

  • Can you give me a more precise help so I know how to do it ?

  • I still don’t understand the problem. You’re having trouble creating routes or links?

  • I’m having a hard time creating the routes

  • Somebody give me a little help if possible

  • is that you’re trying to get $_GET.... you have to use $_SERVER['REQUEST_URI'] ...

Show 7 more comments

1 answer

1

It may be that your 'problem' is more a project issue.

Note that your permission array does not form combinations, with this you need to get the 1st bar that corresponds to a file that will scan bar by bar with a series of if’s.

$permissao = array( 'home' , 'comer' , 'dormir' , '...' );

if(!isset($primeiraUrl) || $primeiraUrl == ''){
    include("conteudos/home.php");
}elseif(isset($primeiraUrl) && in_array($primeiraUrl, $permissao)){
    include("".$directorio."/".$primeiraUrl.".php");
}

This form is very poorly flexible, the ideal is to create a config file that abstracts the routes and a file to validate.

If the URL is http://exemplo.pt/empresa-x, then your variable $primeiraUrl will have the value of enterprise-x, and this is outside its rules in the array $permissao.

A solution that would solve your problem would be to treat the exception as an ad page. Thus, the URL’s http://exemplo.pt/qualquer-nome-de-empresa will be accepted and it will be up to the system to check whether the ad was found or not, and will continue to work for the other URL’s you have, like http://exemplo.pt/comer/, http://exemplo.pt/dormir/.

solution

if( ! isset( $primeiraUrl ) || $primeiraUrl == '' ) {
    include 'conteudos/home.php';
} elseif( in_array( $primeiraUrl , $permissao ) ) {
    include "{$directorio}/{$primeiraUrl}.php";
} else {
    // $primeiraUrl não está no array, posso SUPOR que seja um anuncio
    // include no arquivo que carrega o anuncio
    // include 'anuncio.php';
}

advertising.php

Verifica no DB se `$primeiraUrl` corresponde a algum anúncio
caso contrário, você apresenta sua mensagem de erro.404

OBS, This solves the problem, but it’s not ideal. I recommend looking for routes in PHP


Updating

Doing the routing by .HTACCESS, you would have to define all possible routes, including the order, as in the example below.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# http://exemplo.pt/comer/
RewriteRule ^comer\/$                    index.php?controller=comer&option=index        [QSA,L]

# http://exemplo.pt/comer/camarao/
RewriteRule ^comer\/([a-zA-Z-0-9-_]+)\/  index.php?controller=comer&option=tipo&tipo=$1 [QSA,L]

# http://exemplo.pt/restaurante-do-joao/
RewriteRule ^([a-zA-Z-0-9-_]+)\.html     index.php?controller=anuncio&option=$1         [QSA,L]

The advantage will be that you will not need PHP to combine the possible routes, combining input and output. You will receive via GET the ready parameters.

Note that I alluded to an MVC-based system. Not being your case, then, controller represents a physical file that will work with the option. Everything will start by index.php, then a superficial example would be:


To the input URL http://exemplo.pt/comer/restaurante-do-joao/, you will have

$_GET['controller'] = 'anuncio'
$_GET['option']     = 'restaurante-do-joao'

In his index php. you do the proper checks - which I omitted - and have the corresponding file uploaded to the controller:

//include anuncio.php
include $_GET['controller'] . 'php';

In the case of the example, it will be loaded advertising.php, and in it you use $_GET['option'] to find the company name.

  • Is there a route framework that can be adapted to the project ?

  • You can’t give an example of routes like I would have to do in my project to get an idea I’ve been looking for on the subject but I can’t find anything that can help me

  • @Césarsousa, O Kohana has a class that works with the routes, it is worth taking a look.

  • I had to but and very complicated I will not be able to implement it, I need something simpler that can put this to work because I only need this on the site

  • @Césarsousa, Have you ever thought to define the routes individually on HTACCESS? So you skip the PHP matching process.

  • No I have never done any of this from friendly urls and the first time so and that I am in great difficulty can tell me how I can do it this way ?

  • @Césarsousa, includes the examples doing the routes through HTACCESS. If you don’t understand any just ask that I exemplify better.

  • What if I don’t want to show the file name and show only the corresponding name ? and that name well from the database

  • Can anyone help here

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.