CI routing $this->agent->is_mobile() - php

Is there any way in the CI routing file I can find out if a view is using a mobile?
and if so rotate them to a different page?
I've got two sets of controllers one for mobile and one for desktop but I'd like the urls to always be the same.
I've tried adding the following code to the routes config file but I'm getting an error...
Am I thinking about this totally wrong?
$this->load->library('user_agent');
if ($this->agent->is_browser())
{
$route['default_controller'] = "index";
} elseif ($this->agent->is_mobile())
{
$route['default_controller'] = "m/index";
}
The error I'm getting is Undefined property: CI_Router::$load

From examining the CI system files, it appears that the Loader class is loaded after the Router class, so $this->load doesn't exist yet.
Check out CI Hooks, though: https://ellislab.com/codeigniter/user-guide/general/hooks.html
In addition, you might try using head.js (http://headjs.com/) and defining screen widths for Responsive Design. It enables you to build a web site and change the CSS to change the page depending on the width of the browser. Unless you are aiming at mobile/desktop due to functionality, screen size is the common reason we care what they are on, right? So, if it is screen size, I think the head.js system is the way to go. Then you have only one codebase of server-side stuff to worry about.

Related

Laravel Routes vs Mod-rewrite

So - I'm new to Laravel and we're using version 3.
I have a home page setup & working - say http://dev.mywebsite.com
Now I want to click a link on the page and redirect to a nice SEO friendly URL, but pass in the variables. In straight PHP this is simple - I can make http://dev.mywebsite.com/vacancies/town/page2 rewrite as http://dev.mywebsite.com/?where=town&page=2
But I can't get this to work in Laravel.
I know the full answer is to create controllers & views, but I have all the logic for the display in an included file so don't really want to change... is there any way to do this using either routes or mod-rewrite?
Thanks
Although I strongly recommend you to make a view with that file, you can still use PHP's Output Control to avoid messing with Laravel's rendering flow:
Route::get('your-nice-url-here', function() {
ob_start();
include 'your-raw-php-here';
return Response::make(ob_get_clean());
});

Drupal hook_url_inbound_alter not altering my url

I have in my module named 'categorie' a implementation of:
function categorie_url_inbound_alter(&$result, $path, $path_language) {
if ($path == 'e') {
$result = 'user';
}
}
I'm planning to do something a bit more advanced but I can't even seem to get the basic one working.
With this implementation I expect that if a user goes to mysite.com/e , he gets the user page. But I'm getting a 404.
In this topic:Using module: url_alter and it's hook: hook_url_outbound_alter() they also suggested you need to implement the hook_boot() with nothing in it. However I did implement this hook and this didn't change the behavior.
UPDATE:
I have the "Path" module installed from core and I declared some url aliases in the clean url section. (Maybe this gives a conflict?)
UPDATE2: cleaning the cache didn't do the trick either.
UPDATE3:
I also tries doing the url rewriting in the htaccess file. But when the url was rewritten it destroyed the theming. (https://drupal.stackexchange.com/questions/76475/drupal-does-rewrite-url-but-cant-load-css-themes/76493?noredirect=1#76493). So that's why I'm now trying to do it with the custom module.
What cache did you clear ? It probably not sufficient
to just clear the page cache. You can try a call to
drupal_lookup_path('wipe');
To clear the alias cache. This fixes many problems with the alias system.
Other things to check are the alias database table to be sure that the alias are being
set in the first place.
I'm not 100% sure, cause i'm also very new to drupal. But i think you need to implement hook_url_outbound_alter as well. Good luck. If i find a better answer i'll post it.
Cheers

Problems reading data from Codeigniter Template Partials, and with controller redirection

I have two unrelated Codeigniter problems:
(a) I am trying to redirect a user to the dashboard after logging in successfully. i.e. from controller "auth" to controller "dashboard". For some odd reason, it keeps redirecting to auth/dashboard (404 error since it does not exist).
This is what I did:
//Within the auth controller
if ($this->auth->login($username, $password)) //Auth->login returns boolean
{
redirect('/dashboard', 'refresh');
# redirect('/dashboard', 'location'); // I tried this too
# redirect('/dashboard/index', 'refresh'); // I also tried this!
}
What am I doing wrong, please? It just keeps redirecting to auth/dashboard
(b) I am using Phil Sturgeon's brilliant Codeigniter Template library (without a Parser) and I am unable to receive data sent into a partials file. This data is ONLY needed by the partials file. I have been over the (unfortunately sparse) library documentation, S/O posts on the topic, and also the CI forums to no avail, there seems to be no clear-cut example showing how a partial receives data that a controller assigns to it.
This is how I have been doing it. Please tell me what I must be doing wrong.
// In the controller page
$this->data['user'] = array('info' => 'username', 'value' => 'Cogicero');
$this->data['prefs'] = array('foo' => 'bar');
$this->template
->set_partial('header', 'partials/header', $this->data)
->set_layout('blog')
->build('foobar_view');
And
//In the header partials page
<?php
/* snip */
print_r($data);
print_r($prefs);
print_r($user);
echo $user["info"];
echo $prefs["foo"];
?>
All the above produce "array does not exist" or "undefined variable" errors! How am I meant to be receiving data within the partials view file?
Thanks
EDIT:
Working on a tight deadline so with no solution in sight, I had to abandon Phil Sturgeon's template library and pick up Jens Segers' instead. http://www.jenssegers.be/blog/25/Codeigniter-template-library
It is a little similar to Phil's but for some reason the partials are receiving and rendering my data properly. Also, Sergers' template library is very well documented. All is fine now, so I'll accept my own answer to problem (a). Thanks everyone.
(a) So after a lot of fiddling around I have solved the first problem i.e. the redirects.
In the config file, we have
| If this is not set then CodeIgniter will guess the protocol, domain and
| path to your installation.
|
*/
$config['base_url']=
Apparently, I didn't set a base URL because I'm still on a development local server, I was going to set it to the www address when I upload to the test server. Codeigniter had been guessing all along, but the guess didn't work for my authentication redirects. I set the base url and all redirects were fine.
(b) Now, to see why the partial views are not receiving data. Any help please? :(

Codeigniter Loading external site in default controller

I'm not able to figure this out on my own so here I am asking for your help.
How do I load a website that I already made as a view in the code igniter default controller?
I put my website under a folder name site, and in the default controller I loaded the view site/index , but then in my site there are problems with the includes and redirects... I don't know why, I guess the way the site usually works with redirecting isn't compatible with code igniter style
edit: I guess I would have to turn off CI engine for this site, but I don't know why, because I would still need codeingiter to manage other parts of my application
"CodeIgniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested. To specify a default controller, open your application/config/routes.php file and set this variable:
$route['default_controller'] = 'Blog';
Where Blog is the name of the controller class you want used. If you now load your main index.php file without specifying any URI segments you'll see your Hello World message by default."
http://codeigniter.com/user_guide/general/controllers.html
fragment copied from that link , you should put the controllers classname in that config, not the view
I guess it's better to choose one of these options:
Modify the existing site to a CodeIgniter site.
Keep your site separate from the CodeIgniter site, and just link between the two sites.
The way you are trying to do it seems very useless and causing a lot of extra trouble.
You can simply use the redirect function in your controller. If you supply a full URL you can go to any other page. You will, of course, leave your CI app.
redirect('http://www.example.net/page_in_external_site/');
Try using the APPPATH constant when defining the paths for the includes.
I know it's an old question, but you can try using a view template with an iframe, and you can pass the URL to the src property of the iframe. That way you can display your site inside a view, but still can't get access to the vars passed to the view from your site.
In system/Core/Loader.php change the line 141 to look like this:
$this->_ci_view_paths = array(APPPATH . 'views/' => TRUE, FCPATH => TRUE);
and to get the view is simple:
$this->load->view('application/ PATH_TO_VIEW');

How to integrate Wordpress into Kohana 3

I now need to make a Kohana 3 site have a Wordpress blog.
I've seen Kerkness' Kohana For Wordpress, but it seems to be the opposite of what I want.
Here are the options I have thought of
Style a template to look exactly like the Kohana site (time consuming, non DRY and may not work)
Include the blog within an iframe (ugly as all hell)
cURL the Wordpress pages in. This of course means I will need to create layers between comment posting, etc, which sounds like too much work.
Is there any way I can include a Wordpress blog within an existing Kohana application? Do you have any suggestions?
I found this post detailing the Kohana for Wordpress plugin, but I am still confused as to how it works.
Does it mean from within Wordpress, I can call a Kohana controller? Is this useful to me in my situation?
Oh, I did this a long time ago (actually towards the end of last year).
Assumptions
You are using Wordpress permalinks with mod_rewrite or a similar option.
You don't have register_globals() turned on. Turn it off to ensure Wordpress's global variables don't get removed by Kohana.
Renaming
First, you need to rename the __() function in Kohana. Say, you rename it to __t(). You'd need to replace it everywhere it appears, which if you use an editor like Netbeans that can find usages of a function or method is pretty easy.
Hierarchy
The next decision you need to make is whether you want to load Wordpress inside Kohana or Kohana inside Wordpress. I prefer the latter, which I'm documenting below. I could document the latter if you'd prefer to go that route.
I put the kohana directory in my theme directory.
In your functions.php file of your theme, simply
include TEMPLATEPATH . '/kohana/index.php';
Kohana Configuration
Your Kohana's index.php file also needs some work. Remove the lines that look for install.php as they will load ABSPATH . WPINC . 'install.php' instead and display an error message in your wordpress admin. You also need to change the error_reporting as at the moment Wordpress fails E_STRICT.
You will very likely need to remove the last few lines of your bootstrap (in Kohana) that process the request, and change your init:
Kohana::init(array(
'base_url' => get_bloginfo('home') . '/',
'index_file' => '',
));
In either your Wordpress functions.php file or in your bootstrap, add these lines:
remove_filter('template_redirect', 'redirect_canonical');
add_filter('template_redirect', 'Application::redirect_canonical');
where Application is a class of your choosing.
My code for the Application class (without the class definition) is:
public static function redirect_canonical($requested_url=null, $do_redirect=true)
{
if (is_404() && self::test_url())
{
echo Request::instance()->execute()->send_headers()->response;
exit;
}
redirect_canonical($requested_url, $do_redirect);
}
public static function test_url($url = NULL)
{
if ($url === NULL)
{
$url = str_replace('?'.$_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']);
$url = trim($url, '/');
}
foreach (Route::all() as $route)
{
/* #var $route Route */
if ($params = $route->matches($url))
{
$controller = 'controller_';
if (isset($params['directory']))
{
// Controllers are in a sub-directory
$controller .= strtolower(str_replace('/', '_', $params['directory'])).'_';
}
// Store the controller
$controller .= $params['controller'];
$action = Route::$default_action;
if (isset($params['action']))
{
$action = $params['action'];
}
if (!class_exists($controller))
return false;
if (!(method_exists($controller, 'action_' . $action) || method_exists($controller, '__call')))
return false;
return true;
}
}
return false;
}
which lets Wordpress do it's redirect for any page that may have moved e.g. /about/calendar to /calendar as long as you don't have an about controller and calendar action defined.
So there you have it. Any urls not defined within Wordpress will fall to your defined controller (or use your theme's 404 template).
Additional
This isn't required, but you could put your theme's header.php under your kohana views folder (application or in a module) and from any of your theme files
echo View::factory('header')
You could do the same thing with your footer (or any other files for that matter). In your header.php, you could also do this:
if (isset($title)) echo $title; else wp_title(YOUR_OPTIONS);
That way you could in your controller
echo View::factory('header')->set('title', 'YOUR_TITLE');
To keep urls consistent, you may have to take off the / from the end of Wordpress permalinks so /%year%/%monthnum%/%day%/%postname%/ becomes /%year%/%monthnum%/%day%/%postname%, etc
Please let me know if you need any more help integrating Wordpress and Kohana.
I've actually used wordpress for the CMS of a code igniter site. This is the method i used to pull page content, not blog content, but maybe you can change it up a little to fit your needs.
In my front controller I added the wordpress header file
require('/path/to/wp-blog-header.php');
This gives you access to the 2 functions you'll need
get_page() – Get the page data from the database
wpautop() – Automatically add paragraph tags to page content
To get page data
$page_data = get_page( 4 ); // Where 4 is the page ID in wordpress
If you get this error:
Fatal error: Only variables can be
passed by reference…
You have to do it like this
$page_id = 4;
$page_data = get_page( $page_id );
because of a bug in certain versions of php
Then in the view
<?= wpautop($page_data->post_content) ?>
Hope this helps
EDIT
I installed wordpress at /blog in the filesystem. So wordpress actually runs as a blog normally. I just use this method to grab the pages
This is going to be extremely difficult, because of the way WordPress works. Specifically, it uses global variables all over the place, and because Kohana is scoped, you will not be able to access those variables.
Long story short: what you want is nearly impossible. However, if you get it working (without hacking WP), I would be really interested to see how you did it.
See here: http://www.intuitivity.org/archives/8
I figured it out yesterday :)
Another solution is to keep both Wordpress and Kohana installations completely separate. Then you create a custom Wordpress theme that will pull the header and footer from Kohana (you can create a Kohana controller for that).
Once you have the header and footer in, the blog looks integrated to your website even though it's still a completely separate installation. The advantage is that there's nothing to hack to either Wordpress or Kohana to get it working.
There's some more details about this method in this blog post: Integrating Wordpress into a Kohana application
I always thought this would be relatively easy. That is, to use WordPress as your site's back-end (for the blog part, at least) and use Kohana for serving up posts and pages. If I'm not mistaking, all you would need to do is set up your models (post, comment, page) to gather their data from the WordPress database (with or without ORM) instead of a new one.

Categories