Welcome Guest, Not a member yet? Register   Sign In
how is add subject blog to url with _remap and .htaccess? (dynamic)
#1

[eluser]SaSa[/eluser]
how is add subject blog to url for that page that with _remap and .htaccess.
LIKE:
my url:
Code:
http://localhost/blog?id=16
to
subject: this is CodeIgniter
url:
Code:
www.mysite.com/blog/this is CodeIgniter.

how change this for any dynamic id? and replace $row->subject with hello?

my CI_Controller is:
Code:
<?php
class Blog extends CI_Controller {
    function __construct()
    {
        parent::__construct();                
    }
    
    function _remap( $method )
        {
            // $method contains the second segment of your URI
            switch( $method )
            {
                case 'hello':
                    $this->index();
                    break;
            }
        }
    /**/
    function index()
    {
        
                $g_subject = $this->input->get('id', TRUE);            
                $query = $this->db->get_where('miniblog', array('id' => $g_subject));
                foreach ($query->result() as $row)
                {
                $data = array(
                    'subject' => $row->subject,
                    'title' => $row->title,                
                    'image_path' => $row->image_path,
                    'alt' => $row->alt,
                    'text' => $row->text,
                    'date' => $row->date,
                );
                }
                
                 $this->load->view('miniblog/blog', $data);
                 //add customer size to databe on customer
                
                 //$this->customer_size_model->show();
              
    }
    function ipv6()
    {
        $this->load->view('miniblog/ipv6');
    }
}
?>
my .htaccess is:
Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

I need your help, please help me in this post. Thanks!
#2

[eluser]osci[/eluser]
This is my way of remap in this scenario
In remap I check for existence of title - no spaces or dissallowed chars, use url_title(), and if error show 404.

Code:
function __construct()
   {
      parent::__construct();
      $this->load->model('page_model');
   }

   public function _remap($method, $params = array())
   {
      switch ($method)
      {  //I disallow ie do_ in the start of the title, to have other routes to controller
         case 'do_stuff':
         case 'do_other':
            if (method_exists($this, $method))
            {
               return call_user_func_array(array($this, $method), $params);
            }
            break;
         case 'index':
            return $this->index('home'); //my default page slug
         default:
            $query = $this->page_model->get_content($method)->row();
            if ($query)
            {
               return $this->index($method);
            }
            break;
      }
      show_404();
   }

   function index($slug)
   {
      $query = $this->page_model->get_content($slug)->row();
      if (!$query) {show_404();}
      // rest of code here
   }
#3

[eluser]SaSa[/eluser]
thanks!
Please explain how did you do it?
I do not know how much, what we have in the model? (page_model)
Why are not variable cases?
Etc ...
#4

[eluser]osci[/eluser]
I thought if you looked at the code as a workflow you would get the idea.

Code:
page_model->get_content($method)->row()

$method is the title request as it is returned by blog/title-request

get_content is your query for grabbing your content data and slug, like where tbl.title = 'slug'

Your index does the db stuff while they should be put in your model for MVC.

You should read a little about models and MVC in userguide
#5

[eluser]SaSa[/eluser]
Thanks!
I have problem and need your help:

My CI_ControllerSadblog.php)
Code:
<?php
    class Blog extends CI_Controller {
    function __construct()
    {
        parent::__construct();              
    }
    public function _remap($method, $params = array())
    {
        switch ($method)
        {      //i disallow ie do_ in the start of the title, to have other routes to controller
        case 'do_stuff':
        case 'do_other':
        if (method_exists($this, $method))
        {
            return call_user_func_array(array($this, $method), $params);
        }
        break;
        case 'index':
        return $this->index('home'); //my default page slug
        default:
        $query = $this->home_model->blog($method)->subject();
        if ($query)
        {
            return $this->index($method);
        }
        break;
        }
      show_404();
    }
    
    function index($slug)
    {

        $query = $this->home_model->blog($slug)->subject();
        if (!$query) {show_404();}
        // rest of code here
    }
    }
?>

My CI_MODELSadhome_model.php)- Home_model is autoload.

Code:
<?php
class Home_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
    
    function blog()
    {
        $g_subject = $this->input->get('id', TRUE);            
        $query = $this->db->get_where('miniblog', array('id' => $g_subject));
        foreach ($query->result() as $row)
        {
            $data = array(
                'subject' => $row->subject,
                'title' => $row->title,                
                'image_path' => $row->image_path,
                'alt' => $row->alt,
                'text' => $row->text,
                'date' => $row->date,
            );
        }        
         $this->load->view('miniblog/blog', $data);                        
    }
}
This address 404 Page Not Found:
Code:
http://localhost/index

This address have error:
Code:
http://localhost/blog
error that:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: models/home_model.php

Line Number: 24

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: models/home_model.php

Line Number: 24


Fatal error: Call to a member function subject() on a non-object in D:\xampp\htdocs\application\controllers\blog.php on line 33

what do i do?
#6

[eluser]osci[/eluser]
Your model is wrong.
You load view in model to display?
What is ->subject() ???
You don't pass the slug (title) in the function, in your model you ask for input->get(id)

I think you should read as I stated above about MVC.
#7

[eluser]SaSa[/eluser]
Quote:The _remap function seems some functionality of CI to forward requests from one controller/action to another, and has nothing to do with URLs.
If you want to rewrite URLs you need mod_rewrite. This is not something that CI can do on it's own.

Did is this speech true?
Which is best(_remap or .htaccess) for rewrite URLs?
#8

[eluser]danmontgomery[/eluser]
remap and htaccess are complicated (and not ideal) solutions for a simple problem. Use the routing functionality built into codeigniter.

http://ellislab.com/codeigniter/user-gui...uting.html

Code:
$route['blog/(:any)'] = 'blog_controller/some_method/$1';

Then just check for the slug which you store in the database along with the post

Code:
public function some_method($slug)
{
    $query = $this->db->where('slug', $slug)->get('blog_table');
    // etc
}
#9

[eluser]SaSa[/eluser]
Thanks!
Want show (my subject) $row->subject to url no id.
I going to address bottom and no error:
Code:
http://localhost/blog?id=13
But i going to address bottom or any addressSaddsdsds-> this is my subject)
Code:
http://localhost/blog/dsdsds
or
http://localhost/blog/...
this have error:
Code:
<?php class Blog extends CI_Controller {
    function __construct()
    {
    parent::__construct();              
    }
    public function blog($g_subject)
    {
        $query = $this->db->where('subject', $g_subject)->get('miniblog');
        // etc
    }
    function index()
    {
    $g_subject = $this->input->get('id', TRUE);            
    $query = $this->db->get_where('miniblog', array('id' => $g_subject));
    foreach ($query->result() as $row)
    {
        $data = array(
        'subject_b' => $row->subject,
        'title_b' => $row->title,                
        'image_path_b' => $row->image_path,
        'alt_b' => $row->alt,
        'text_b' => $row->text,
        'date_b' => $row->date,
        );
    }
    $this->load->view('miniblog/blog', $data);
    }
}
?>

Code:
$route['blog/(:any)'] = 'blog/blog/$1';

error:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: controllers/blog.php

Line Number: 26

Line Number: 4

'>
Line Number: 5

'>

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: date_b

Filename: core/Loader.php(679) : eval()'d code

Line Number: 94


A PHP Error was encountered

Severity: Notice

Message: Undefined variable: title_b

Filename: core/Loader.php(679) : eval()'d code

Line Number: 98


A PHP Error was encountered

Severity: Notice

Message: Undefined variable: subject_b

Filename: core/Loader.php(679) : eval()'d code

Line Number: 98



A PHP Error was encountered

Severity: Notice

Message: Undefined variable: image_path_b

Filename: core/Loader.php(679) : eval()'d code

Line Number: 102

" alt="
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: alt_b

Filename: core/Loader.php(679) : eval()'d code

Line Number: 102

">
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: text_b

Filename: core/Loader.php(679) : eval()'d code

Line Number: 103
why?
#10

[eluser]SaSa[/eluser]
Please help...!!!




Theme © iAndrew 2016 - Forum software by © MyBB