Ver Mensaje Individual
  #3 (permalink)  
Antiguo 05/07/2011, 06:14
Avatar de Italico76
Italico76
 
Fecha de Ingreso: abril-2007
Mensajes: 3.303
Antigüedad: 17 años, 1 mes
Puntos: 292
Respuesta: Optimizar url en CoideIgniter

Hoola MIKTRV

Cita:
misite.com/noticias/titulo-de-la-noticia
Para pasar de noticias/detalle/1 a noticias/detalle/titulo-de-la-noticia todo lo que necesitas es pedir por el ultimo segmento de la URL ('titulo-de-la-noticia) en vez de pedir por el ID

Para que eliminar el /detalle/ o sea... ocultar el nombre de la accion (funcion)......eso lo haces mediante .htaccess

Cita:
Imagino que en mi base de datos no se puede repetir ningún título,
No lo imagines! utiliza un clave UNIQUE en tu DB y si el titulo se repite... al hacer el insert de la noticia al final agregale algo.. por ejemplo.. la fecha: titulo-de-la-noticia-2011-04-09

Aca lo que yo he hecho.....

Blog controller (tu 'noticias') llamado blog.php (en tu caso deberia ser noticias.php y cambiarle el nombre a la clase)

Código PHP:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/* Controller */
class blog extends CI_Controller {    

    private 
$data=array();
  
    function 
__construct()
    {        
        
parent::__construct();                        
                            
        
$this->load->library('Blog_lib');                            
        
$this->data['post_list'] = $this->blog_lib->get_last_entries(3);    
        
        
$this->output->cache(2);
    }
    
    public function 
index(){        
    }

    function 
post($uri){
       
$post_ay $this->blog_lib->get_entry_by_uri ($uri);          
                    
        
$this->data['content'] = $this->strings->format_post($post_ay);
                     
       
$this->load->view('vista'$this->data);    
    }
    
     
}

en /libraries uso esta biblioteca (Blog_lib.php) en vez de un modelo:

Código PHP:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/* Library  */
class Blog_lib {
   private 
$CI

   public function 
__construct(){
     
$this->CI =& get_instance();     
   }
   
   function 
get_last_entries($n=10)
    {                
      
$query $this->CI->db
            
->select('post_uri,post_title, post_content,post_image')
            ->
get('blog'$n);  
      return 
$query->result();        
    }
    
    function 
get_entry_by_uri ($uri){
        
      
$query $this->CI->db
            
->select('post_title, post_content,post_image,post_uri')
            ->
where('post_uri',$uri)->get('blog'1);
        
      
$post  $query->result(); 
              
     
      if ((
$post[0]->post_uri==$post[0]->post_content) AND (strpos($post[0]->post_content,'.html',0) !== false) ){
      
        
$post[0]->post_content file_get_contents(base_url()."application/rentabilizaweb/controllers/posts/{$uri}");
                
      }
     
      return 
$post;              
    }
    
    function 
get_entry_by_id ($id){
        
      
$query $this->CI->db
            
->select('post_title, post_content,post_image,post_uri')
            ->
where('id',$id)->get('blog'1);
        
      return 
$query->result(); 
    }  

// end class
(podras ver que el sistema carga tambien noticias guardadas como archivos con extension .html en controllers/posts)

En mi caso....formateo los posts... insertandoles una imagen......y lo hago asi:

en /libraries/ se llama Strings.php
Código PHP:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Strings {   

  
// devuelve el primer parrafo
  
public function one_p($string) { 
    
$article explode("\n"$string); 
    
$parrafo $article[0]; 
    return( 
$parrafo );  
  } 
  
  public function 
format_post($post_ay){ 
    
    
$CI =& get_instance();     
  
    if (
strlen($post_ay[0]->post_image)>0){
      
$img img('application/rentabilizaweb/views/images/post_icons/'.$post_ay[0]->post_image).br(2);
    }else{
      
$img='';
    }      
  
    return 
heading($post_ay[0]->post_title,2).$img.$CI->typography->auto_typography($post_ay[0]->post_content);
  }

  
// end class
La tabla se llama ' blog' y es asi:


Cita:
CREATE TABLE IF NOT EXISTS `blog` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`post_title` varchar(100) NOT NULL,
`post_uri` varchar(100) NOT NULL,
`post_content` text NOT NULL,
`post_image` varchar(200) NOT NULL,
`post_keywords` varchar(200) NOT NULL,
`post_description` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `titulo` (`post_title`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
Podes verlo funcionar aca
__________________
Salu2!

Última edición por Italico76; 05/07/2011 a las 06:25