I am using CodeIgniter to design a website, and I am having trouble getting anchor to work correctly.
Here is how I am using it. It is used in a file in views folder of application in codeigniter.
<?php echo anchor('/profile', 'PROFILE'); ?>
My intention is to have anchor load the profile controller, which is in the controllers of application in codeigniter. However, when I clink on the link, it says that the the file is not found.
I autoload the url helper functions alreader, and other functions from that file are working, like site_url() .
I don't know what I could have missed? Do you ave any suggestions? This is my second project with codeigniter, so I am still learning.
EDIT: Yes, I am following the naming convention in codeigniter, and the file has an index function. I tried without the forward slash and it still gives the same result.
The HTML link that this produces is localhost/profile. This is what I Should get right? Since for codeigniter, it's url/controller/function. I did a mod rewrite to remove index.php from the url, but that shouldn't be a problem, should it? I'll try and check the base url again.
Are you sure you need the leading forward slash?
Have you tried:
<?php echo anchor('profile', 'PROFILE'); ?>
You would only be using the slash if you are trying to get into the "profile" sub folder inside your controllers folder.
What helps me when urls get confusing is to type into the browser the full url to the path I want to get to (to make sure I'm not getting a 404, etc). From there you can start from the end and go back to see what you are missing.
But you generally want to start with a controller name and add the function you are calling like:
anchor(controllerName/functionName)
See CodeIgniter URL Helper for examples.
EDIT: also - you may need to check what your config base_url is set to. if there's fancy .htaccess stuff happening, your base_url needs to be set properly.
There is a very simple solution go to config/constants and define SITE_URL there and set its value to
http://localhost/myprojectname/index.php.
Then use it here like this.
<?php echo anchor(SITE_URL.'/profile', 'PROFILE'); ?>
Related
I'm new to codeIgniter. Can you tell me, how to link to other website url in codeIgniter?
I tired to do like this:
google
But instead it gives me this
google
It always refers to the base url, even though I don't echo base_url(); on the href.
Thanks
When creating links to external pages, you must prefix the URL with either http:// or https:// , like this:
google
Technically this has little to do with CodeIgniter, but a difference between relative and absolute URLs.
If you did want to use CodeIgniter to generate the link, do this:
$this->load->helper('url');
echo anchor('http://www.google.com', 'Google');
I am working on a small project and all of my urls are written without base_url() function. So I was thinking what does those base_url() functions help with my urls. I know it will prefix the url based on configs file preferences. But simply I wanna ask
What is the advantage of using them ? and whats the disadvantage of not using them ?
The main advantage is that; when you want to migrate your project to online or in a different server; you won't need to edit your hard-coded links. The base_url() will make your inside links to get their base url dynamically.
echo base_url("blog/post/123");
will return
http://example.com/blog/post/123
in example.com and will return
http://jms.com/blog/post/123
in jms.com
base_url is needed when you create login/signup with email verification or forgot password functionality (Verifying by clicking the links like this http://yourdomain.com/auth/userid/code).
This is only one example, may have lot of uses.
the difference of base_url() and site_url() is that
both returns the url of your site but site_url() returns your site url, with attached index.php or on what you have configured $config['index_page'] = ''
in application/config/config.php
example www.yoursite.com/chicken/turkey
when using base_url(): Returns your site base URL, as specified in your config file.
you will get wwww.yoursite.com
when using site_url():
you will get www.yoursite.com/index.php
site_url() is usefull if you haven't change your .htaccess and you still access your controllers with index.php
base_url() is usefull when linking your css,js,or media stuffs. Granting that you still use index.php when accessing your controller/method
Its needed every-where, or at least we should use everywhere. Do you know, absolute url is far better then relative url? So, when you are using an absolute urls for all links on your site, instead of hard coded root domain name, you can use the "base_url()" function. Moreover, CI reactor is smart enough to detect the original base url, so you even don't need to enter it in config file.
I usually using the base_url() for the correct paths to the static files (css, js, img) files and the site_url() for the links to pages. site_url() will return with url_suffix witch could be set in application/config/config.php file
Advantage:-if you not use base_url() and you want to switch directory/server of your site , you should change the path all over the project.
if you use base_url(), just change it at onece in config.php ,effect on all site.
I'm using Phil Sturgeon's Template Library in my CI installation. When I try to put view partials' files in a theme folder, they do get called properly, but if I try to echo a $template variable in them, I get an error message saying that $template variable isn't defined. If I place those same view partials in the root views folder, $template is echoed properly.
How can I have view partials inside a theme and pass $template to them at the same time?
I was encountering the same problem. This looks like its a problem with some fancy hacks Phil had to use for compatibility with Modular Extensions.
If you're not using a parser, simply turn off parser_enabled in the template config file. Its the first option. Then it'll work.
If you're using a parser, you'll have to dig into the library. The problem is in the _load_view function, check lines 725-728. Something needs to be done here to get the variables to the parsed template.
Hi I created a codeigniter project but when I click on a link to one of my functions, example add user, I get redirected to the main page of my local host XAMPP installation instead of being taken to the correct application url. What can be the problem? Thanks
Have you set the base URL in the CI configuration? file projectname/application/config/config.php? This might be the problem... I guess your project isn't on the webroot, but your base URL is missing the /projectname/ part.
$config['base_url'] = 'http://example.com/projectname/';
How do you create the link (can you show us the code)? If you are not using the URLHelper take a look at urlHelper.
I am just guessing, but maybe you are missing the Controller name (you have to load the UrlHelper), e.g.:
Link to the controllers method
or (see Jordan Arsenault's comment below on the usage of the site_url call for better performance):
<?= anchor('/name_of_the_controller/method_to_invoke', 'Link to the controllers method'); ?>
I hope this helps.
Im building my first application using CodeIgniter, i need a bit of advice.
There are 2 functions that do the same thing and i was wondering which is the best to use.
Ok, so usually when im building a site, i link to the homepage by a simple / but if building on a directory, it will take back to the root public_html directory, with codeigniter i have found both
site_url()
and
base_url()
but, they both seem to do the same thing .. Just wondering if theres any difference, which one is better to use, etc etc.
Cheers,
If you are using index.php
site_url()
will include the index.php , and
base_url()
will not include it.
If you are creating a url to pass, use
site_url('images/img.png')
otherwise...
base_url().'images/img.png'