CodeIgniter - Correct way to link to another page in a view

て烟熏妆下的殇ゞ 提交于 2019-12-17 08:47:59

问题


I was wondering if someone could tell me the correct way to link to another page from within a view.

Is there a function for this or is it just the usual about

Cheers,


回答1:


I assume you are meaning "internally" within your application.

you can create your own <a> tag and insert a url in the href like this

<a href="<?php echo site_url('controller/function/uri') ?>">Link</a>

OR you can use the URL helper this way to generate an <a> tag

anchor(uri segments, text, attributes)

So... to use it...

<?php echo anchor('controller/function/uri', 'Link', 'class="link-class"') ?>

and that will generate

<a href="http://domain.com/index.php/controller/function/uri" class="link-class">Link</a>

For the additional commented question

I would use my first example

so...

<a href="<?php echo site_url('controller/function') ?>"><img src="<?php echo base_url() ?>img/path/file.jpg" /></a>

for images (and other assets) I wouldn't put the file path within the php, I would just echo the base_url() and then add the path normally.




回答2:


The best way is to use the following code:

<a href="<?php echo base_url() ?>directory_name/filename.php">Link</a>



回答3:


you can also use PHP short tag to make it shorter. here's an example

<a href="<?= site_url('controller/function'); ?>Contacts</a>

or use the built in anchor function of CI.




回答4:


Best and easiest way is to use anchor tag in CodeIgniter like eg.

<?php 
    $this->load->helper('url'); 
    echo anchor('name_of_controller_file/function_name_if_any', 'Sign Out', array('class' => '', 'id' => '')); 
?>

Refer https://www.codeigniter.com/user_guide/helpers/url_helper.html for details

This will surely work.




回答5:


you can also use this code

//test" class="btn btn-primary pull-right">




回答6:


<a href="<?php echo site_url('controller/function'); ?>Compose</a>

<a href="<?php echo site_url('controller/function'); ?>Inbox</a>

<a href="<?php echo site_url('controller/function'); ?>Outbox</a>

<a href="<?php echo site_url('controller/function'); ?>logout</a>

<a href="<?php echo site_url('controller/function'); ?>logout</a>


来源:https://stackoverflow.com/questions/5205155/codeigniter-correct-way-to-link-to-another-page-in-a-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!