How to set menu links (href) in codeigniter? - codeigniter

I'm trying to convert my core php project into CodeIgniter.
I have set the baseurl for the project in the config file i.e: http://localhost/myproject/
When I'm trying to set menu anchor href i need to pass controller_name/action_name
<a href='controller_name/action_name'> Tag_Name
I'm not able to set directly the action name for the same controller.
<a href='action_name'> Tag_Name
I have also loaded the URL helper in my Controller.

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

Go with
Link
or
<?php echo anchor('controller/function/uri', 'Link', 'class="link-class"') ?>
Any of the two should be ok.
Also refer to this link.
Or you could use base_url() function to get the value set in the config file for $config['base_url'].

Just use below code
Tag_name
Thanks You

Related

Codeigniter URL for navigation

I can't figure out how to do the url links.
Basically I have my navigation bar, and I don't know which CodeIgniter URL code to use and how to implement it.
Is what I'm doing here right?:
<?php $this->load->helper('url'); ?>
<li>About Us</li>
I tried to do an anchor like this, but when I load the page it just turns up blank:
<?php echo anchor('views/about.html', 'About Us', title='About Us'); ?>
What am I doing wrong?
There are two ways to make links:
CodeIgniter helper style:
<?php echo anchor('about', 'About us', 'title="About us link"'); ?>
More common HTML with URL echo:
About us
Both will output:
About us
Though if I understand what you are trying to achieve, your mistake is elsewhere.
You don't include the views part, as your URL should point to the controller, not a view. The only case is if you have a controller named views.
CodeIgniter is set up so that it doesn't include file extensions like .html in URL's by default. It does if you've set them up in your config file in $config['url_suffix'] = '';, which is null by default.
See if you've made any of these mistakes.
That is another way on how you do the URL if you are using the URL helper in CI. You should try this, make the base_url() as the value for href. Try this,
About Us
You have to try like this
About Us
or you can give like
About Us
and in the "about" function you put
$this->load->view('about');
but i think the firstone will works for you fine.

codeigniter 2.1.0 Routing difficulties

I can not load controller with
http://localhost/index.php?controller/method
But if i does not use '?' and try to load a controller from view then duplicate URL produce.
As an example of above, if i add this link in form action in view then result url will be
http://localhost/index.php/controller/index.php/controller/method
How to solve it? I did not use htaccess file
Use the site_url() function or, as #JohnFable stated, use the form_open function.
<form method="post" action="<?= form_open('controller/method'); ?>">
or
Controller/Method
or
<?= form_open('controller/method'); ?>
This will ensure that your "base url", i.e. http://localhost is prepended correctly to the URL you want to view, i.e. http://localhost/index.php/controller/method or if you have set it up to, http://localhost/controller/method
Gavin

codeigniter path issue

I am trying to link my view page to another controller.
my test_view.php page
//this page address is base_url/controller1/function1
<a href='controller1/function2'> test </a>
If i click, the page address will be base_url/controller1/function1/controller1/function2 which is not my desire.
my controller
//the first function1 is to show my test_view page
function function1 (){
$this->load->view('test_view');
}
//I can't get to this function2 with the link I used
function function2 (){
$this->load->view('funny');
}
Anyone could help me about this? Thanks a lot.
Sure--you just need to tell CodeIgniter to display the path:
<a href="<?php echo site_url("controller1/function2");?>">
One thing: This displays the absolute path of your site as defined in your config, not the relative path.
I prefer relative paths, so I like to create a universal function called site_path to do the same thing without the absolute URL. I include it in one of my universally loaded libraries and it looks something like this:
function site_path($url) {
return "/$url";
}
The benefit of this is that, if I initially develop the site in a subdirectory, I can set site_path to return "/subdirectory/$url" and then just remove the subdirectory once I launch.
It's linking to a relative URL, you need to start with a '/' to use the web root
<a href='/controller1/function2'> test </a>
you can use following code in test_view.php page,
<a href='<?php echo base_url();?>controller1/function2'> test </a>

How to get particular page URL in magento

I would like to get a page's URL key in Magento.
For example, I have a CMS page called "What's New" with the identifier (or URL key) "whats_new". Its correct URL is therefore http://mysite.com/whats_new
Currently I use this code to echo its location:
<?php echo Mage::getBaseUrl();?>whats_new
I feel it's bad practice because its identifier (or URL key) is administrable; if its URL key or identifier changes then the link will break. What is the proper way to echo its dynamic URL key? Perhaps something similar to Wordpress's get_permalink('10')?
I think this will do what you want:
<?php echo Mage::helper('cms/page')->getPageUrl( $pageId ) ?>
Replace $pageId with the correct id for the page you are linking to and it should work.
Try this
<?php echo $this->getUrl('whats_new');?>
If you need to add url key dynamically then
<?php echo $this->getUrl($yourDynamicVariable);?>
of course you must implement the features that you need to fill the variable if url key is changed
You shoud use <?php echo Mage::getUrl('page-url.html); ?>
In CMS Page
{{store _direct="url_key"}}
If you want in .phtml file then
<?php echo Mage::helper('cms/page')->getPageUrl('url_key') ?>
Mage::getUrl(null, array('_direct' => $page->getIdentifier()));
It is also possible to retrieve the CMS page URL using the page identifier like,
<?php echo Mage::helper('cms/page')->getPageUrl('cms_page_identifier') ?>
You shoud use
{{store direct_url="whats_new/"}}
<?php echo $this->getUrl('whats_new');?>

CodeIgniter view anchor tag prepending host

In my view I'm trying to create an anchor tag but CodeIgniter is prepending my entire host before what I specify as href, therefor making it invalid.
I put this
My File
I get
My File
EDIT 3
This is my view, I realise i should not be calling a function in my view but in this case I had little option as something needs to get applied for each data item in the loop, I will try to change this but thats beyond my problem right now.
Wesley: I checked the soruce and it displays correctly in the source but in the browser it preappends the host so I guess this has nothing to do with code igniter afterall! How do I make sure it doesn't happen?
<td><?php
$this->ci = &get_instance();
echo $currentData["field_one"] . " - Log"; ?>
</td>
EDIT 4
my html source
<a href="file:://///\\myhost.local.com\120">
View Log
</a>
my url address bar
http://myhost.local.com/myhostlocal/index.php/level/one/type/b/cc/ee/
the url it goes too when I mouse over the href
http:///myhostlocal/index.php/level/one/type/b/cc/ee/file:://///\\myhost.local.com\120
Looks like the browser is treating "file:://///" as a relative path. Why do you have 2 colons, do you need both? Removing one will provide a valid protocol, and the browser will start treating it as an absolute path.

Resources