如何在wordign中像codeigniter一样自动生成永久链接?

时间:2018-03-14 09:41:17

标签: php wordpress codeigniter url permalinks

我正在使用此系统中的CodeIgniter应用程序我可以选择从管理面板创建动态页面titlecontent两者都存储在数据库中。但是我想在permalink发布后像WordPress一样在CodeIgniter中自动生成page网址?

例如

the page title name test 
the function should be created 
http://localhost/codeigniter/test after form data stored in the database

页面控制器

class Pages extends CIF_Controller {

public $layout = 'full';
public $module = 'pages';
public $model = 'Pages_model';

public function __construct() {
    parent::__construct();
    $this->load->model($this->model);
    $this->_primary_key = $this->{$this->model}->_primary_keys[0];
    $this->permission();
}


public function index() {

     $this->{$this->model}->custom_select = 'pages.*, menu.title as menu';
     $this->{$this->model}->joins = array(
        'menu' => array('menu.menu_id = pages.menu_id', 'inner')

    );

    $data['items'] = $this->{$this->model}->get();
    $this->load->view($this->module . '/index', $data);

}

public function manage($id = FALSE) {
    $data = array();

     $this->{$this->model}->custom_select = 'pages.*, menu.title as menu';
     $this->{$this->model}->joins = array(
        'menu' => array('menu.menu_id = pages.menu_id', 'inner')

    );

    if ($id) {
        $this->{$this->model}->{$this->_primary_key} = $id;
        $data['item'] = $this->{$this->model}->get();
        if (!$data['item'])
            show_404();
    } else {
        $data['item'] = new Std();

    }
    $this->load->library("form_validation");
    $this->form_validation->set_rules('title', 'title', 'trim|required');
    $this->form_validation->set_rules('menu_id', 'select menu', 'trim|required');
    $this->form_validation->set_rules('content', 'content', 'trim|required');
    $this->form_validation->set_rules('link', 'link', 'trim|required');

    if ($this->form_validation->run() == FALSE)
        $this->load->view($this->module . '/manage', $data);

    else {
        $this->{$this->model}->title = $this->input->post('title');
        $this->{$this->model}->menu_id = $this->input->post('menu_id');
        $this->{$this->model}->content = $this->input->post('content');
        $this->{$this->model}->menulink = $this->input->post('link');
        $this->{$this->model}->save();
        redirect('admin/' . $this->module);
    }
}

public function delete($id = false) {
    if (!$id)
        show_404();
    $this->{$this->model}->{$this->_primary_key} = $id;
    $data['item'] = $this->{$this->model}->get();
    if (!$data['item'])
        show_404();
    $this->{$this->model}->delete();
    redirect('admin/' . $this->module);
}
}

1 个答案:

答案 0 :(得分:0)

我在我的模型中使用了一个方法(在你的情况下是'Pages_model'),它存储页面以生成url,它将标题作为输入并返回一个唯一的URL,然后我将其添加到数据库字段'url' 。

需要从空格和不需要的字符中清除URL。由于我的语言是瑞典语,我们还想重写一些字符,等等。然后我们需要检查url是不重复的,如果是这种情况我只是在网址中添加uniqid(),你可以想到其他方法。以下是如何编写方法的示例:

function make_url($title)
{
    // rewrite special characters
    $url = str_replace('é','e',$title);
    $url = str_replace('å','a',$url);
    $url = str_replace('ä','a',$url);
    $url = str_replace('ö','o',$url);
    $url = str_replace('Å','A',$url);
    $url = str_replace('Ä','A',$url);
    $url = str_replace('Ö','O',$url);
    // rewrite spaces and multiple dashes to single dashes
    $url = str_replace(' ','-',$url);
    $url = str_replace('--','-',$url);
    $url = str_replace('---','-',$url);
    $url = str_replace('----','-',$url);
    // remove all other characters
    $url = preg_replace('/[^A-Za-z0-9\-]/','',$url);
    // make url lowercase
    $url = strtolower($url);

    // check if url already used (in 'pages' table)
    $q = $this->db->where('url',$url)->get('pages');
    if ($q->num_rows()): 
        $url = $url . '-' . uniqid();
    endif;
    return $url;
}

因此,添加此模型方法,然后修改Pages_model->save()方法,使用$this->make_url($title)将您的发布标题添加到数据库的数据库中。