Debiprasad
Debiprasad

Reputation: 6183

Which one is the best way to get the URL in CodeIginter using base_url() URL helper?

As per the user guide, we can get the URL in CodeIgniter in following way using URL helper:

echo base_url('admin/profile');

But I saw one developer is using code like this:

echo base_url() . 'admin/profile';

Which one do you think will be faster?

I think both does not make any difference, as under the hood, it must be concatenating that string passed in the parameter. To follow a good coding standard, the former method could be better. Am I right or wrong?

Upvotes: 0

Views: 50

Answers (4)

Ankit Balyan
Ankit Balyan

Reputation: 1329

Either you use echo base_url('admin/profile'); or echo base_url() . 'admin/profile';. Both generate the same result. The difference is in the processing, talking about speed, there might be a negligible difference, as base_url('admin/profile') is a core function which basically takes the parameters and concatenates the string and returns, on other hand if we do not give any parameter to it and concatenate the echoing string is the same thing. It is user preference, I like to use passing the string in base_url function.

Upvotes: 0

GouxLord
GouxLord

Reputation: 86

Code Igniter base_url() call too

function base_url($uri = '')
    {
        return $this->slash_item('base_url').ltrim($this->_uri_string($uri), '/');
    }

So base_url().$string is not the same as base_url($string).

Also you may be interested in override base_url method in the future and have other specifics process on URI.

So a good standard approach is:

echo base_url('admin/profile');

Upvotes: 1

Pablo Ezequiel Leone
Pablo Ezequiel Leone

Reputation: 1387

I'm not used to use CodeIgniter but...

echo base_url() . 'admin/profile';

Makes more sense... As a developer I don't have to know what is under the hood in base_url(), I only read the name of the function and I can guess what is the return. The other way...

echo base_url('admin/profile');

As web developer who is not used to code with CodeIgniter, if there is an error in the code, I have to go to the documentation or debug the framework to see what happen, which is time consuming.

This is my opinion...

Upvotes: 0

Hammad
Hammad

Reputation: 2137

Both approaches are correct and it depends what you are trying to do. base_url() will just print the website base_url() that you have set in your config file, whereas when you specify some string either by passing it to as parameter or concatenating with it the result is same.

For example:

base_url="http://localhost/mywebsite/"; //Defined in config file

base_url().'mycontroller/myfunction';// This will print http://localhost/mywebsite/mycontroller/myfunction

base_url('mycontroller/myfunction');// This will also print http://localhost/mywebsite/mycontroller/myfunction

So you can use any approach you are comfortable working with, technically both are same. As far speed is concerned, the former is faster.

Upvotes: 0

Related Questions