Welcome Guest, Not a member yet? Register   Sign In
Little independent applications
#1

[eluser]breaddes[/eluser]
How can I create independent applications like Tell a friend, Newsletter Subscription, Banner etc. which I can add to my views? It should be like components or modules that are flexible and encapsulated. I am not aware of any solution yet.
#2

[eluser]InsiteFX[/eluser]
Search Widgets on the forum.

If you a modular system search for HMVC

InsiteFX
#3

[eluser]Jônatan fróes[/eluser]
I suggest Modular CI
#4

[eluser]breaddes[/eluser]
I looked into some of that solutions but I have a basic problem with all of them.

If I have a link on that independent widget I want to point it only to the widget itself. Let's say I have a link that gives further information:

Code:
<a href="widget/givefurtherinformation">Call</a>

It's clear that this will not run within a closed widget (except javascript + ajax). It will redirect the Browser to that link which I don't want actually. I only want the widget to change. The rest of the site should maintain as is.

What are ways to manage that? I want to call the same site again, but let the widget know that it needs to do something else.

I appreciate any help or ideas!!
#5

[eluser]breaddes[/eluser]
Is it an illusion that this can work in PHP? I am used to it from Java and it saves me a lot of time because it's just reusable components that can be integrated where ever I want. They work for themselves and might have public methods to get information or to set information.

That seemingly means that if I want to change a newsletter module in CI I have to change it in every place I use it.
#6

[eluser]slowgary[/eluser]
Hi breaddes,

It looks like just a confusion of terminology. If you're looking for reusable components on your site, I'd recommend just making them views in themselves (or a library call that loads a view, depending on your needs.)

For instance, you may have a controller that loads your homepage like so:
Code:
$this->load->view('header');
$this->load->view('home');
$this->load->view('footer');

To add your widget, just load the widget view with the page:

Code:
$this->load->view('header');
$this->load->view('home');
$this->load->view('my_widget_1');
$this->load->view('my_widget_2');
$this->load->view('footer');

Those widget views can have CSS classes that cause them to float to the right column of your pages. If the widgets need to make database calls or do some heavy calculating, make a library instead and include it in /system/application/librabries. Then, in your controller:
Code:
$this->load->library('my_widget_1');

$this->load->view('header');
$this->load->view('home');
$this->my_widget_1->view();
$this->load->view('footer');
Or something along those lines. Then you can include your widgets on any pages that you'd like:
Code:
$this->load->library('my_widget_1');
$this->load->library('my_widget_2');

//homepage
$this->load->view('header');
$this->load->view('home');
$this->my_widget_1->view();
$this->load->view('footer');

//search page
$this->load->view('header');
$this->load->view('search');
$this->my_widget_1->view();
$this->load->view('footer');

//category page
$this->load->view('header');
$this->load->view('category');
$this->my_widget_1->view();
$this->my_widget_2->view();
$this->load->view('footer');


I hope this helps.
#7

[eluser]Buso[/eluser]
[quote author="breaddes" date="1285697233"]I looked into some of that solutions but I have a basic problem with all of them.

If I have a link on that independent widget I want to point it only to the widget itself. Let's say I have a link that gives further information:

Code:
<a href="widget/givefurtherinformation">Call</a>

It's clear that this will not run within a closed widget (except javascript + ajax). It will redirect the Browser to that link which I don't want actually. I only want the widget to change. The rest of the site should maintain as is.

What are ways to manage that? I want to call the same site again, but let the widget know that it needs to do something else.

I appreciate any help or ideas!![/quote]
Php is server-side, so if you want the hyperlink to do something else, you have to talk to the browser with javascript. What I do is write js abstractions, so I can have that kind of functionallity with a single php function.
Eg: echo widget('my_cool_js_widget');
I had to build my own modules/widgets system though.
#8

[eluser]breaddes[/eluser]
[quote author="slowgary" date="1285709787"]
Code:
$this->load->library('my_widget_1');

$this->load->view('header');
$this->load->view('home');
$this->my_widget_1->view();
$this->load->view('footer');
[/quote]

Thank you for your great examples!

Let's say my controller where all this is happening is the default CI welcome controller. My url would look like:
Code:
http://localhost/welcome

My widget has a link in it to load another view of the widget. How would you call that other widget view? I could do:

Code:
http://localhost/welcome/methodToChangeWidgetView

But that would not be reusable. The widget would point to a certain method in a certain controller. So it should actually call something like:

Code:
http://localhost/widget/methodToChangeWidgetView

The problem with that is I am loosing my welcome page. I am just out of it.

Can you see the trouble?
#9

[eluser]slowgary[/eluser]
I see breaddes. There are several options. Buso is right, you can use Javascript to interact with the server, then you won't even have to change pages in your widgets. If you're not experienced with Javascript, using a library like jQuery might make things easier.

If the Javascript route seems too daunting, you can make your widget links all point to the current page and just pass some data along with the HTTP request. Since CodeIgniter prefers that we don't use GET data, you'd want to make your widgets use forms instead of links.

So if you were creating a voting widget, there would be a few parts...

The Initial View
Code:
<div class='right_column'>
     What is your favorite color?
     &lt;form action="&lt;?php echo $_SERVER['PHP_SELF']?&gt;"&gt;
          <div>
               <label>
                    &lt;input type='radio' name='widget_vote_color' value='red'/&gt; Red
               </label>
               <label>
                    &lt;input type='radio' name='widget_vote_color' value='green'/&gt; Green
               </label>
               <label>
                    &lt;input type='radio' name='widget_vote_color' value='blue'/&gt; Blue
               </label>
               <label>
                    &lt;input type='submit' value='Vote'/&gt;
               </label>
          </div>
     &lt;/form&gt;
</div>

The "already voted view"
Code:
<div class='right_column'>
     <strong>Thank you for voting!</strong>
</div>

And the voting widget library
Code:
// pseudocode
class VotingWidget extends Controller
{
     function view()
     {
          if(isset($_COOKIE['somecookie']) AND $_COOKIE['somecookie'] == 'already_voted') {
               $this->load->view('voting_widget_already_voted');
          } else if($vote = $this->input->post('widget_vote_color')) {
               //store the vote in the database and set $_COOKIE['somecookie'] = 'already_voted';
               $this->load->view('voting_widget_already_voted');
          } else {
               $this->load->view('voting_widget_initial_view');
          }
     }
}


None of this code has been tested, and I'm not even sure if you can extend the Controller class in a library, but it should hopefully serve as an illustration of how to accomplish something like this. Personally, I would take the Javascript route as it will provide a better user experience, less server load, and less page loads for the user.

I hope this helps.
#10

[eluser]breaddes[/eluser]
Thank you slowgary. I had a look at HMVC and the approach is similar.

Code:
if($_POST)
      modules::run('subscripe/save');
else
      modules::run('subscripe');

I am not really happy with it because you have to give up the principal to call controllers and methods via request. I mean that the uri determines what is going to happen if you click on something.

I wonder if there a routing or _remap hacks to work this out.




Theme © iAndrew 2016 - Forum software by © MyBB