Welcome Guest, Not a member yet? Register   Sign In
Loading a page without controller
#1

[eluser]Olivier69[/eluser]
Hello,

I have a problem with codeigniter, i need to load an URL without using browser.

I mean i need to call a controller and a function without loading page, and put the result in a variable.

The problem is that my URL contains variable and looks like :

controller / function / id / othervariable

I need to do this because when somebody is logged, i check if it's profile is complete before he can access to application.

If not, i make a redirection on profile form to ask him to complete.

All interface is in Ajax, everything works, but i can't load this form properly during connexion.

I precise that i dont wan't to load with JS for many resons, i would like to load a view
Code:
$data = array(
              'title_DesktopNavbar'       => '',
              'desktopNavbar'             => '',
              'title_UnderDesktopHeader'  => lang('title'),
              'menu'                      => '',
              'desktopWrapperLeft'        => HERE I NEED RESULT OF URL controller/function/id/othervariable,
              'desktopWrapperRight'       => lang('user_explain')
);

I even tryed :
Code:
$url =  'user/user_update/' . $this->ci->_userdata()->id . '/none';

$d= read_file($url);

Thanks for your help.
Olivier

PS : sorry for my English, I'm French
#2

[eluser]Olivier69[/eluser]
Nobody knows how to call a controller and it's function with param, without calling it throw the browser ?

It's a big problem not to be able to call this controller, and impossible to call the model directly, without asking controller before witch makes different treatments.

Thanks
#3

[eluser]WanWizard[/eluser]
What do you mean, without the browser? From the commandline?

If this is about Ajax, there is no difference between an ajax and a browser request, they are both http requests.
#4

[eluser]Olivier69[/eluser]
No, i mean to put the result in a variable, before to call a view

I tryed successfully :

Code:
$answer = call_user_func_array(array(__NAMESPACE__ .'\user', 'user_update'), array($this->_userdata()->id, 'none'));

OR

$answer = $this->user_update($user_id, $xml);

$data = array(
              'title_DesktopNavbar'       => '',
              'desktopNavbar'             => '',
              'title_UnderDesktopHeader'  => lang('title'),
              'menu'                      => '',
              'desktopWrapperLeft'        => $answer,
              'desktopWrapperRight'       => lang('user_explain')
);
The problem is that content is displayed before the view and i can't find a solution.

I know how to return data as a string rather than sending it to the browser :
Code:
$string = $this->load->view('myfile', '', true);

I would like to do the same, but in this case i must call a controller with params, and not just a view file.

Thanks for help, i hope it's clear
#5

[eluser]KingSkippus[/eluser]
I think he wants to pipe the results of one controller into a variable in another controller. I would suggest against this. It sounds like the renderable content from the first controller really needs to be moved to a view that is accessible from the first and second controller.

If you absolutely must have the output from one controller in a variable in another controller, it's not an ideal solution, but using Php's cURL library to actually fetch the content might be an option.
#6

[eluser]Prensil Technologies Pvt Ltd.[/eluser]
Hello Olivier,

You can use curl request to call the url without browser.
Please refer http://php.net/manual/en/book.curl.php

Regards,
Prensil Technologies Pvt. Ltd.
http://www.prensil.com
#7

[eluser]mddd[/eluser]
If you need the result of some piece of code, to use as a variable in another controller, then you should put that code in a model or a library. And call it from the controller where it is going to be loaded.

I agree with KingSkippus, loading the result of one controller into another is a bad way of working.
#8

[eluser]Olivier69[/eluser]
Ok, thanks for answers.

In fact, i can't use CURL (i tryed) because your are logged to do this action, so it doesn't work because CURL doesn't understand session.

What is my problem :

With CMS multi user, when they log, users can manage their website. If they didn't accept Conditions, they are first redirected on the page to accept them. No problem, it's a simple view, and it doesn't need particular treatments "to print" (users can't register alone, their account are created by another user with bigest rank, or admin, so they must agree with conditions on the first connexion, or if they changed since their last connexion).

Then, if profile is not complete, they also must be redirected on profile page, to complete (in France, you must provide to justice informations about users from such a service, because it will be commercial CMS).

So i need to impose them to complete their profile in case of requirement from justice, and even for billing.

The problem is that to load form profile, you need to call throw controller a variable with user ID, and a second one to ask to load "the form as php". The same form is used by admins to update members, but in this case, it is loaded in XML with ajax, to refresh different parts of the window.

Conclusion : i need to call a controller before to send the result in a view only for this simple action. The controller makes different treatments before to call the model, and i can't call only model, it would be to easy ;-)

I hope i'm clear.

I tryed many method (curl, read_file... => no session - redirect in JS with ajax loading, but their is a problem with infinite loop that i tryed to correct with sending session data to stop the loop => It is not clean........................).

I can't find a simple solution.

Thanks
#9

[eluser]Olivier69[/eluser]
OK, i found a solution, not very nice, but just to load profile update if non complete, it will be ok.

CMS Admin can't works without JS active in browser (it's a real choice), so it will not be a problem.

Here is the solution :

Code:
$id = $this->ci->_userdata()->id;

$answer = '<a href='user/profiler/$id/none' class="ajax" id="profiler" style="display: none;"></a>&lt; script type="text/javascript" &gt;$(document).ready(function() { $("#profiler").click(); });< /script >';

$data = array(
              'title_DesktopNavbar'       => '',
              'desktopNavbar'             => '',
              'title_UnderDesktopHeader'  => lang('title'),
              'menu'                      => '',
              'desktopWrapperLeft'        => $answer,
              'desktopWrapperRight'       => lang('user_explain')
);

// My function witch generate full view (header, footer...)
$this->ci->load_param->load_view_manager($data);

In $answer, there are some syntax errors ' instead " and &lt; script &gt; to ovoid filters of the forum, witch cut some parts of code. -code- is very bad here and doesn't function well, generating many shorts code and errors !!

In fact, i print a display none link and i simulate a click to launch my Ajax function, to print the profile form.

It works well, it is not very nice, but after many hours, i found a compromise that i hope it can help other developpers.

On this way, i can call my controller and load result without problem.

Thanks for help ;-)
#10

[eluser]smilie[/eluser]
Just a quick thought:

Code:
$id['id'] = $this->ci->_userdata()->id;

$answer = $this->load->view('your_page',$id,TRUE);

$data = array(
              'title_DesktopNavbar'       => '',
              'desktopNavbar'             => '',
              'title_UnderDesktopHeader'  => lang('title'),
              'menu'                      => '',
              'desktopWrapperLeft'        => $answer,
              'desktopWrapperRight'       => lang('user_explain')
);

// My function witch generate full view (header, footer...)
$this->ci->load_param->load_view_manager($data);

And in "your_page" (which is in views) you put HTML:
Code:
<a href='user/profiler/$id/none' class="ajax" id="profiler" style="display: none;"></a>&lt; script type="text/javascript" &gt;$(document).ready(function() { $("#profiler").click(); });< /script >

This way you get in answer HTML that you need :-)

TRUE as 3rd param will parse your view file without showing it on the screen :-)

Cheers,
Smilie




Theme © iAndrew 2016 - Forum software by © MyBB