Codeigniter - Correct Way to Link to Another Page in a View

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

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://example.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.

How to go to another page in codeigniter using href link

Change like this..

<a class="catname" href="<?php echo base_url();?>/welcome/architect">Architect</a>

Diffference between base and site url:

echo base_url(); // http://example.com/website
echo site_url(); // http://example.com/website/index.php

I think your htaccess is a problem

Steps To Remove index.php using .htaccess:-

Step:-1 Open the file config.php located in application/config path. Find and Replace the below code in config.php file.

// Find the below code

$config['index_page'] = "index.php"

// Remove index.php

$config['index_page'] = ""
Step:-2 Go to your CodeIgniter folder and create .htaccess file.

Path:

Your_website_folder/
application/
assets/
system/
user_guide/
.htaccess <--------- this file
index.php
license.txt
Step:-3 Write below code in .htaccess file

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Step:-4 In some cases the default setting for uri_protocol does not work properly. To solve this issue just open the file config.php located in application/config and then find and replace the code as:

// Find the below code

$config['uri_protocol'] = "AUTO"

// Replace it as

$config['uri_protocol'] = "REQUEST_URI"

How to link to another pages in codeigniter by clicking an anchor tags?

By what you show here you may want to set a route to catch all for this controller. In your config routes.php file add a line:

$route['PageController(/:any)'] = "PageController";

then in your controller index method you would parse out uri segments like so, if applicable...

   function index(){
$arg_1 = $this->uri->segment(1);
$arg_2 = $this->uri->segment(2);
//etc.

$this->loadPageHeader();
$this->loadMenuHeader();
$this->loadAdvertisingVideo();
$this->loadMainContent();
$this->loadSidebar();
$this->loadPageFooter();
}

URL segments are useful if you want one method to handle all requests for this controller, but may not be necessary in your case. I don't know enough about your scenario. If I missed something and you have more questions, feel free to ask and I may update.

give link to a view in codeigniter

Don't forget to load $this->load->helper('url'); and think problem in base url in Codeginter.
Go to application->config->config.php and change base url example as http://yourbaseurl/

Codeigniter url set to another page

base_url() will echo:

http://localhost/project

where as site_url() will echo:

http://localhost/project/index.php

You want to get to http://localhost/project/index.php/aboutus but with base_url() you're only getting to http://localhost/project/aboutus which is giving you the error.

You can do two things,

this:

<a href="<?php echo base_url('index.php/aboutus'); ?>">AboutUs</a>

which means adding the index.php before aboutus

or this:

<a href="<?php echo site_url('aboutus'); ?>">AboutUs</a>

which means changing base_url() to site_url().

Make sure that you are loading the helper in the controller:

$this->load->helper(url);

Or in application/config/autoload.php go to the line which says:

$autoload['helper'] = array();

and do this:

$autoload['helper'] = array('url');

and it will be included in every controller you now have.

If you have short tags enabled you can write your a tag like this:

<a href="<?=site_url('aboutus');?>">About Us</a>

or if you have the url helper you can write it like this:

echo anchor('aboutus', 'About Us', 'title="About Us"');

How to link to a specific part of the page with CodeIgniter

you can make route for specific footer

//inside config/routes.php
$route['footer'] = 'controller/method_name';
//controller
function method_name(){
$this->load->view('includes/footer');
}
//view
<a href="<?php echo base_url('auth/logout'); ?>">QuienesSomos</a>

How to set menu links (href) in codeigniter?

please make sure you have added this->load->helper('url'); in controller or you can add in config->autoload.

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


Related Topics



Leave a reply



Submit