Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have created REST API in codeigniter. REST sever created in codeigniter 3.0 and REST client created codeigniter 2.x.x.

I have wrote simple code for login. After login client created dashboard page. So for creating dashboard page I am sending so many request to REST server and it's taking too much time.

Below is the function for send request to server for dashboard. The function called after successfully login. So can please review my code and tell me is it correct way ? I have no idea about REST so and my system is too slow

public function show_dashboard() 
    {   
        $rest_url_module_names="http://MyAPI/Dashboard_api/module_names/token/".$this->session->userdata('userToken');
        $data_module_names = $this->curl->simple_get($rest_url_module_names);
        $data_module_names=json_decode($data_module_names,true);
        if($data_module_names['status']=='success')
        {
            $data['module_names'] = $data_module_names['data'];
        }

        $rest_url_single_emp="http://MyAPI/Dashboard_api/single_emp/token/".$this->session->userdata('userToken');
        $data_single_emp = $this->curl->simple_get($rest_url_single_emp);
        $data_single_emp=json_decode($data_single_emp,true);
        if($data_single_emp['status']=='success')
        {
            $data['single_emp'] = $data_single_emp['data'];
        }

        $rest_url_department_nm="http://MyAPI/Dashboard_api/department_nm/token/".$this->session->userdata('userToken');
        $data_department_nm = $this->curl->simple_get($rest_url_department_nm);
        $data_department_nm=json_decode($data_department_nm,true);
        if($data_department_nm['status']=='success')
        {
            $data['department_nm'] = $data_department_nm['data'];
        }

        $rest_url_designation_nm="http://MyAPI/Dashboard_api/designation_nm/token/".$this->session->userdata('userToken');
        $data_designation_nm = $this->curl->simple_get($rest_url_designation_nm);
        $data_designation_nm=json_decode($data_designation_nm,true);
        if($data_designation_nm['status']=='success')
        {
            $data['designation_nm'] = $data_designation_nm['data'];
        }

        $rest_url_supervisor_nm="http://MyAPI/Dashboard_api/supervisor_nm/token/".$this->session->userdata('userToken');
        $data_supervisor_nm = $this->curl->simple_get($rest_url_supervisor_nm);
        $data_supervisor_nm=json_decode($data_supervisor_nm,true);
        if($data_supervisor_nm['status']=='success')
        {
            $data['supervisor_nm'] = $data_supervisor_nm['data'];
        }

        $rest_url_sub_ordinates_nm="http://MyAPI/Dashboard_api/sub_ordinates_nm/token/".$this->session->userdata('userToken');
        $data_sub_ordinates_nm = $this->curl->simple_get($rest_url_sub_ordinates_nm);
        $data_sub_ordinates_nm=json_decode($data_sub_ordinates_nm,true);
        if($data_sub_ordinates_nm['status']=='success')
        {
            $data['sub_ordinates_nm'] = $data_sub_ordinates_nm['data'];
        }

        $rest_url_profile_pic="http://MyAPI/Dashboard_api/profile_pic/token/".$this->session->userdata('userToken');
        $data_profile_pic = $this->curl->simple_get($rest_url_profile_pic);
        $data_profile_pic=json_decode($data_profile_pic,true);
        if($data_profile_pic['status']=='success')
        {
            $data['profile_pic'] = $data_profile_pic['data'];
        }

        $rest_url_leave_type_dtls="http://MyAPI/Dashboard_api/leave_type_dtls/token/".$this->session->userdata('userToken');
        $data_leave_type_dtls = $this->curl->simple_get($rest_url_leave_type_dtls);
        $data_leave_type_dtls=json_decode($data_leave_type_dtls,true);
        if($data_leave_type_dtls['status']=='success')
        {
            $data['leave_type_dtls'] = $data_leave_type_dtls['data'];
        }

        $rest_url_All_emp_pending_leave_lists="http://MyAPI/Dashboard_api/all_emp_pending_leave_lists/token/".$this->session->userdata('userToken');
        $data_All_emp_pending_leave_lists = $this->curl->simple_get($rest_url_All_emp_pending_leave_lists);
        $data_All_emp_pending_leave_lists=json_decode($data_All_emp_pending_leave_lists,true);
        if($data_All_emp_pending_leave_lists['status']=='success')
        {
            $data['All_emp_pending_leave_lists'] = $data_All_emp_pending_leave_lists['data'];
        }

        $rest_url_general_applying_leave_rule_dtls="http://MyAPI/Dashboard_api/general_applying_leave_rule_dtls/token/".$this->session->userdata('userToken');
        $data_general_applying_leave_rule_dtls = $this->curl->simple_get($rest_url_general_applying_leave_rule_dtls);
        $data_general_applying_leave_rule_dtls=json_decode($data_general_applying_leave_rule_dtls,true);
        if($data_general_applying_leave_rule_dtls['status']=='success')
        {
            $data['general_applying_leave_rule_dtls'] = $data_general_applying_leave_rule_dtls['data'];
        }

        $rest_url_weekend="http://MyAPI/Dashboard_api/weekend/a_weekend/Every/token/".$this->session->userdata('userToken');
        $data_weekend = $this->curl->simple_get($rest_url_weekend);
        $data_weekend=json_decode($data_weekend,true);
        if($data_weekend['status']=='success')
        {
            $data['weekend'] = $data_weekend['data'];
        }

        $rest_url_First_weekoff="http://MyAPI/Dashboard_api/First_weekoff/a_weekend/First/token/".$this->session->userdata('userToken');
        $data_First_weekoff = $this->curl->simple_get($rest_url_First_weekoff);
        $data_First_weekoff=json_decode($data_First_weekoff,true);
        if($data_First_weekoff['status']=='success')
        {
            $data['First_weekoff'] = $data_First_weekoff['data'];
        }

        $rest_url_department_dtls="http://MyAPI/Department_api/department_dtls/token/".$this->session->userdata('userToken');
        $data_department_dtls = $this->curl->simple_get($rest_url_department_dtls);
        $data_department_dtls=json_decode($data_department_dtls,true);
        if($data_department_dtls['status']=='success')
        {
            $data['department_dtls'] = $data_department_dtls['data'];
        }



        $rest_url_parentmoduleIdex="http://MyAPI/Dashboard_api/parentmoduleIdex/Idex/Dashboard";
        $data_parentmoduleIdex = $this->curl->simple_get($rest_url_parentmoduleIdex);
        $data_parentmoduleIdex=json_decode($data_parentmoduleIdex,true);
        if($data_parentmoduleIdex['status']=='success')
        {
            $data['parentmoduleIdex'] = $data_parentmoduleIdex['data'];
        }

        $sub_module_menu_selection = array();
        $sub_module_menu_selection['sub_module_menu_selection'] = 'Dashboard';

        $this->session->set_userdata($sub_module_menu_selection);



        $this->load->view('pages/header',$data); //included header
        $this->load->view('pages/sidebar_menu',$data); //included sidebar
        $this->load->view('dashboard/home',$data);
        $this->load->view('pages/footer',$data); //included footer
    }

In every controller I have added header,sidebar_menu and footer. Means repeating code. Can you please tell me how to manage repeating code ?

share|improve this question

What's happening here is that if each request takes 0.3 seconds (pretty fast), the whole page takes 14 * 0.3 = 4.2 seconds to load (slow-ish).

In cases like this, there's two things you could do.

  1. Run each request asynchronously.

  2. Alter the API to give you everything.

By running the requests asynchronously, you're not waiting for the response of one request to continue to the next. Like this, you could run your 14 requests in parallel; bringing load times down to the slowest request at best.

By altering the API to give you everything you need you'd be looking at possibly a pretty big code change, but you could make a single request and receive everything. It wouldn't be very REST-y.

Personally, I'd recommend option one, as altering the API means that if you need more data you're going to get stuck like this again - drastically so, even: again, if one request took 0.5 seconds, and you were forced to add a second request, you would see page loads double. It's also going to be less work, I'd imagine.


Aside from the slow api, your code is very repetitive. If you made one function for sending out an api call and passed in the URL, the data object to fill, and the key it should use, you could reduce this function to 14 function calls loading data and the couple things you do at the bottom.

share|improve this answer
    
Thanks for reply. Means use common function for send request instead of $this->curl->simple_get() use multiple time ? – Hkachhia yesterday
    
@Hkachhia yes: I just edited my answer to reply to your question edit – oliverpool yesterday

As Pimgd stated, all your request are synchronous (and wait that the previous one terminates before executing further).

To make the calls asynchronous I see two possibilities:

If you know that your clients have Javascript activated, I would recommend you to go for the first version.


Regarding your edit:

Every time you perform:

$rest_url=...;
$this->curl->simple_get...;
json_decode...;
if(..=='success')
  ...

You could create two functions for this:

private function request_api_data($method)
{   
    $rest_url="http://MyAPI/Dashboard_api/".$method. "/token/".$this->session->userdata('userToken');
    $data = $this->curl->simple_get($rest_url);
    $data = json_decode($data, true);
    if($data['status']=='success'){
        return $data['data'];
    } else {
        return null;
    }
}

private function gather_api_data($methods)
{
    $all_data = [];
    foreach($methods as $method){
        $data = $this->request_api_data($method);
        if(!empty($data)){
            $all_data[$method] = $data;
        }
    }
    return $all_data;
}

But if you want to perform them in parallel, you will have to:

  • generate all the URLs
  • pass them to the curl_multi
  • have a feedback which updates the $all_data depending on the request which just completed.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.