CodeIgniter: Can't Get My New Controller/View To Show - php

I am learning how to use codeIgniter as my php framework. I am reading through the documentation and watching the intro video and just generally following along with the first tutorial, but it's not working for me.
I have created a controller called "test.php" and a view called "test_view". The controller for this class is exactly like "welcome.php" and the view file just has some static html. However, when I go to index.php/test I get a 404 error.
I have also tried manipulating the original welcome files so that instead of calling a view it just echos "testing", yet I still see the original welcome message! I've tried clearing my browsing cash and refreshing, but to no avail.
Any suggestions? Thanks.
Edit: Here's the code for controllers/test.php
<?php
class Test extends Controller {
//Just trying to get it to echo test
public function index()
{
echo "test";
//$this->load->view('test_view');
}
}
?>

Try looking at this page in the documentation - this might solve your problem.
This basically means you should try typing index.php?/test/ instead (notice the question-mark).

First of all, check the above link. Might be useful.
If not, then...
Try changing the default controller in the config file ('routes.php') to something else (probably, to 'test'), then try loading index.php. Just to test whether the whole system works (or not).
Check whether mod_rewrite is loaded (in your server .conf-file, if you're using Apache).
Try using the latest build of the framework. AFAIK, the name of the controller class for now is "CI_Controller".
Finally, try removing the word 'public' before the declaration of the function. AFAIR, CI enable you to make private functions in controllers just by using prefix (which is set in the config file) at the beginning of the name of the function (thus making all the other functions public).
But most certainly the problem is with the mod_rewrite. If not, try debugging with die('Page found'); instead of echo - this will allow you to track possible redirects on the page.

Related

PHP Functions from included files don't execute on Web Server

I am in the process of migrating a site from my personal dev server onto Windstream's business hosting server. I've already run into the issue of having developed using PHP 5.4 only to find out that my static functions won't work on WS's 5.1.4 installation. I've since fixed those issues and am not facing one that I can't seem to find any help for on the internet.
All of the static functions I was using have been rewritten as functions outside the class scope. Instead of having
class Product{
...
public static function myFunction(){}
...
}
I now have
function myFunction(){}
class Product{...}
in my included Product.php file.
However, when I try to call myFunction() from my code, nothing happens. I know the nothingness comes from WS's error handling, but the point is, the function isn't working. To verify this, I have taken the following steps:
Inserted the line echo "entered included"; immediately following the <?php in Product.php. This prints "entered included" on the index page, indicating that my include is working. I have done the same thing before the final ?> with the same results, so I don't think it's getting hung up inside the included file.
I have changed myFunction() in the included file to be simply
function myFunction(){echo "myFunction works";}
A call to myFunction() still makes nothing happen.
I have moved myFunction() to the including file (myFunction() now lives in index.php instead of Product.php; index includes Product.php). This time, myFunction() executes without issue.
To my 'hack it til it does what it should' sensibilities, this tells me that the server is having a problem with functions that are declared in files that are included; honestly, though, I have absolutely no clue what's going on, and any help would be appreciated. Their website is currently down, and they were expecting it to only be offline for a day, so I'll try pretty much anything short of sacrificing a fatted calf.
I know I should be posting more specific code, but since this is a customer's actual website, I'm trying to put as little of the actual code out here as is possible. I'm happy to append specific sections of code to this entry as they are requested for clarification.
Thanks in advance for your consideration.
#Rottingham: First, thanks for the 3v4l link. Second, my assumption about static methods in 5.4 vs 5.1.4 came from this line of php.net's page on static members and methods:
"As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static)."
src - http://www.php.net/manual/en/language.oop5.static.php
Since my version and the server version were on different sides of the 5.3 mark mentioned, I incorrectly assumed that this was my problem.
Third, when I get in from my day job, I'll update my code to show errors and update this post if a solution has not yet been found.
Ultimately, my problem isn't with using static methods (since I don't have them anymore) but with using any function that is declared in an included .php file.

How to use Ajax with jQuery in Laravel 4?

I am brand new to Laravel 4 and seeing if I can convert my website over to it (previously written without a framework). I can't get AJAX (via jQuery) to interact properly with my controller.
So firstly, the controller I am working in is called IndexController.php and has a function called types which is shown below:
class IndexController extends BaseController {
// Other controller functions
public function types($brand) {
$types = DB::select('select * from aircraft_types where brand_id = ?', array($brand));
echo json_encode($types);
}
}
The function returns the data from the database (in JSON format). I have then created a route to this controller and function as follows:
Route::get('types/{id}', array('uses'=>'IndexController#types'));
I have doubled checked that the route and function are working by going to //localhost/lavarel/public/types/1 and indeed it returns the correct JSON data.
The jquery function in question is below:
function updateTypes($brand) {
$("#type").append('<option value="test">Test...</option>'); // This executes correctly
$.getJSON('/types/'+$brand, function(data) {
$("#type").append('<option value="test 2">Test 2...</option>'); // This does not
// Some other JSON related code
});
To test where the function works, I have inserted two lines which edit the item I am using. The function is being called correctly as the first 'Test' option is being appended. However, it never seems to activate the callback function as the second line of test code is not executed.
I suspect the issue is the URL I am providing to JavaScript '/types/'+$brand. I have seen in some tutorials a BASE var used before the URL I provide, but I don't see why my code above wouldn't work. Any pointers?
Can anyone explain to me where I am going wrong?
Your base path to your laravel project is localhost/laravel/public/ but your AJAX-request goes to just localhost. There're some methods for fixing this.
Method 1:
This is the most preffered method. (in my opinion)
Instead of using nginx, apache for starting a web server, you can use PHP's built in web server.
Open a terminal window (or cmd in Windows), cd into the main directory for your project (the one with vendor, app and public directories) and type the command php artisan serve. This will create a PHP-server on localhost:8000 and your base path will be /.
There's a lot of options, for example php artisan help serve" if you want to see them all.
After you've done so your code should work.
Method 2
If you want to use nginx or apache you should add a virtualhost for your project.
That can be done though the configs.
Apache: http://httpd.apache.org/docs/current/vhosts/examples.html
Nginx: http://wiki.nginx.org/ServerBlockExample
After that you should add a new entry to the hosts file.
Method 3
As you said you could add a BASE variable before the '/types/'+$brand.
Your request goes to localhost/types/$brand.
It should go to localhost/laravel/public/types/$brand.
Just replace '/types/'$brand with '/laravel/public/types'+$brand
Use HTML 'base' meta tag
<!doctype html>
<html lang="en">
<head>
...
<base href="{{ URL::to('/') }}"/>
...
Declaring this tag, you ensure that every relative URL will be based on project's real, absolute URL (http://localhost/my-laravel-project/public/), not on Localhost's URL (http://localhost)

TinyMVC always shows 'default' controller

I was following documentaion till the end, tested one html template with smarty and then cut it. Then I found out that controllers do not work as expected – whatever name I create in myapp/conrollers, 'hello.php' for example, that contains class described in docs, i. e.
class Hello_Controller extends TinyMVC_Controller
{
function index()
{
echo "Hello World.";
}
function time()
{
echo "The time is now.";
}
}
I can’t show it. So the name of the file is a prefix for the controller class name, all seems to be ok here, but going to /index.php/hello returns what is in 'default.php'. I’ve even tried to change default controller to 'hello' in myapp/configs/application.php by setting $config['default_controller'], but the framework behaves like if it’s always work with the 'default.php'. There is no errors on screen or in logs (I checked twice every option in configs of my web server and interpreter), I totally don’t know what to do with that goddamn piece of crap, I can’t even write on its forum because waiting for ‘administration approval’ for several days.
I had to dig inside of the framework to find an answer. And it is when it checks for a controller file it uses file_exists() which do not respect include path. Googling ‘TinyMVC+file_exists’ gave me link to that topic, where is written that they had fixed it in SVN version.

Codeigniter app returns blank output in CLI

I'm setting up my cron job controller that will only run within the CLI, I've not started with anything built, just in the testing phase with CI's examples. However, when running it I get no output or anything else, just a new line, this is the command I ran:
root#serv$ php /var/www/ci/index.php tools message
root#serv
As you can see in the second line, I get no output, just a new line to run an command but I don't understand why and I cannot debug it. The code contains this:
<?php
class Tools extends CI_Controller {
public function message($to = 'World')
{
echo "Hello {$to}!".PHP_EOL;
}
}
?>
In my configuration file, the $config['uri_protocol'] is set to AUTO so this does not seem to be the problem.
How can I debug this? What are the options that I may need to deal with?
I also have display_errors on and error_reporting on to E_ALL.
I found the problem, it was a problem with a redirect('domain.com'); exit; I had on an autoloaded library, because it was matching against the domain in a database, that way CLI doesn't serve a domain when it detects, hence I included a redirect('domain.com') with an exit which is why I'm not seeing any output.
I also encountered this while i was playing w/ cli for codeigniter. It took me a while to troubleshoot the issue and found out that I forgot to except my controller on my login model which runs a redirect function.
This may be a silly suggestion, but might as well give it a shot... #lolwut, what if instead of using "echo", maybe you have to "return" the output?
I have an application following this guide: http://codeigniter.com/wiki/Category:Advanced::CronScript running (and producing output). CI was only 1.7.2 when I did so, but it might still hold
(CodeIgniter 2.2.0)
Add a route in /application/config/routes.php
for...
<?php
class Cron extends CI_Controller
{
public function process_dumps()
{
echo "Processing dumps..." . PHP_EOL;
}
}
?>
add...
$route['cron/process_dumps'] = 'cron/process_dumps'
Without this line there was no output on the CLI!
I think you can find the answer in the Codeigniter Wiki.
By calling to the cron.php with parameters controller and function, and then define CRON_CI_INDEX to the refer to the file path of your main index.php.
For example,
php /var/www/ci/cron.php --run=/tools/message

How to include a codeigniter project into another php file?

I have a project based on codeigniter. And I should use one class that extended from a codeigniter controller in another php file. But I didn't find the solution about how to teach another php file to see whole CI-project. Beyond that needed class can not inherit when i call it from other place.
I'm not 100% sure if this helps get you in the right direction, but kudos if it does!
Codeigniter routes the application depending on the environment state of the URI. What you need to do is set the environment and include the index view file like so:
$_SERVER["REQUEST_URI"] = "cms/2";
//Set GET action,method params etc
require_once "path/to/index.php";
When you load CI Index file it reads the SERVER Variable and others which you may have to find and execute the controller and method, I would also advise that you modify the library/view file as it may exit upon output causing your script to exit.
Also you may wis hto look into ob_start() to catch the buffer.

Categories