Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access others CI's Controllers (not Welcome)? #3

Open
netsenior opened this issue Oct 23, 2013 · 9 comments
Open

How to access others CI's Controllers (not Welcome)? #3

netsenior opened this issue Oct 23, 2013 · 9 comments

Comments

@netsenior
Copy link

Hi Dan,

how to navigate through the pages of a CI system?

I mean, how to control if its a WP URL or a CI URL?

thanks again

@dynamodan
Copy link
Owner

Alright, this depends on some careful cooperation of the WordPressIgniter settings and CodeIgniter's config/routes.php settings. Since both WordPress and CodeIgniter each have their own 404 handling methods, they both need some attention. In the plugin settings, there are two checkboxes: "Divert 404s to WordPressIgniter" and "CodeIgniter grabs all SEO urls (use with caution!!)".

"Divert 404s to WordPressIgniter" UNCHECKED:
Any url that WordPress can't resolve gets the WordPress 404 page rendered out, even if it was a valid CodeIgniter url. If it was a valid CodeIgniter url, that CodeIgniter url and route gets executed normally, but the user is still shown the WordPress 404 page, whatever it may be in the configured template, etc.

"Divert 404s to WordPressIgniter" CHECKED:
Any url that WordPress can't resolve gets the CodeIgniter output rendered out using the configured Page Override page.

"CodeIgniter grabs all SEO urls" UNCHECKED:
The plugin caches the contents of $_SERVER['REQUEST_URI'] and temporarily sets it to '/' to force CodeIgniter to load its default controller and index function. By default this is 'welcome/index'. CodeIgniter's routing is doing practically nothing in this case. Using this setting unchecked makes CodeIgniter pretty useless unless you have everything happening in the default controller's index function, or maybe you're doing routing with the url parameters the old fashioned way etc.

"CodeIgniter grabs all SEO urls" CHECKED:
Here's where things get the most useful. But, you must have a sort of a "do nothing" 404 override set in your config/routes.php. This function will get called every time a WordPress post, category, or anything is accessed by a SEO url. If you have this setting checked, but no $route['404_override'] set, then CodeIgniter's 404 page will show even on valid WordPress urls (because they're not valid CodeIgniter urls!!).

Sorry if I'm not good at explaining, some of this you're probably going to need to experiment with yourself, but trust me I've spent many long hours with lots of different methods to arrive at what we have now. Probably the bottom line understanding is that when the plugin is enabled, CodeIgniter ALWAYS runs. Depending on those two settings described above, and the configuration in config/routes.php, CodeIgniter WILL do one of these 4 things:

  1. Run the default controller and function set by $route['default_controller']
  2. Run the controller and function determined by the url path.
  3. Run the "catch all" controller and function set by $route['404_override']
  4. Show its own 404 error, completely overriding WordPress

Again, with this plugin enabled, CodeIgniter WILL do one of the 4 things above, even if its output isn't rendered out to the user. This theoretically can result in some special circumstances. Say you have a path collision, perchance that you have a CodeIgniter controller and method (or custom route) that responds to /categories/uncategorized . The specific CI controller and method will run, even though WordPress renders out its own category view. This is for two reasons: 1. It wasn't a WordPress 404 and 2. It wasn't a CodeIgniter 404. So just be careful to avoid this kind of CodeIgniter routing collisions (unless you really intended it).

@netsenior
Copy link
Author

Hey Dan

thanks for the explanation, and for the good job. I will expriment those options carefully later.

cheers

@ansmn1974
Copy link

Dear Dan,
Thank you for the great plug-in. I just wanted to inform you of a bug:

I created the controll sum.php that has the function my_sum($a,$b)

I wanted to try to call a controller with parameters .
I called it from the url: http://localhost/wp/sum/my_sum/1/2/

I got the result that is shown in the image
screen shot 2014-09-01 at 19 30 00

It shows that it throws an < h1 > tag with the word wordpressigniter in it
Source code that being rendered by the browser is like the following:
< h1 class="entry-title">WordPressIgnition!

But thank you for the nice plug-in

@dollardad
Copy link

Thanks for this great plugin, just working my way through the code.
Some useful tips for anyone wanting to use this.

  1. All WordPress functions, hooks and filters are available to you in your Codeigniter Controllers and Models.
  2. You can use either or both $db (need to setup your database ci config as usual) and $wpdb ( global $wpdb). Really handy if you want to keep database code in two tables.
  3. You must remove headers and footers in you ci views ( use the wordpress template system ).
  4. You can enqueue scripts through either your wp theme or CI app.

@superlaziale75
Copy link

Dear Dan,

Great job!

could you please help us to understand what do you mean when you say "have a sort of a "do nothing" 404 override".
How can do that?
How can redirect the WP URL to WP?

Thank you very much.

@hakimny
Copy link

hakimny commented May 7, 2015

@dollardad , @dynamodan

How did you manage to use wordpress functions in CI, i tired calling them directly , they do not generate an error but i have no output.

Any help on this would be highly appreciated.

thx
Hakim

@dynamodan
Copy link
Owner

It could be a problem with output buffering. I tend to use logging to tackle this kind of problem. Can you write something to a log to see what the functions are doing, i.e. by assigning their return value to a $variable and then echoing it to a log? I know your hosting may not give you very ready access to the logs but I depend heavily on logging in most aspects of my work. :)

@dynamodan
Copy link
Owner

@superlaziale75

Think of CodeIgniter always running even if it's not a match for the currently loading page in WordPress. It will throw its own internal 404 error on every page that doesn't match a CodeIgniter route. You just won't see anything on pages that WordPressIgniter doesn't catch as a CI-integrated page. I'm more comfortable knowing exactly where CodeIgniter's execution path will go in those situations so I like to use a "catch all, do nothing 404 page".

@dynamodan
Copy link
Owner

@ansmn1974

You are probably echoing the function's output. Try assigning the function's return value to the $CI_OUTPUT global variable. (Remember to say global $CI_OUTPUT in your function.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants