Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Building Websites with PHP Slim Basics & Twig Templates Including & Running Slim

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Is Slim compatable with localhost?

Hello :)

I've been trying to download, install and run the slim Framework for this course but I keep running into problems, despite downloading the files to my project Area in Composer/slim.

Warning: require(Slim/Slim.php): failed to open stream: No such file or directory in C:\xampp\htdocs\composer\index.php on line 7

Fatal error: require(): Failed opening required 'Slim/Slim.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\composer\index.php on line 7

For ease of use I've been doing this in localhost. I can put all the files I need in there but I don't get the 404 page that Hampton gets. What am I missing? :)

Here's my php code.

<?php 

//require autoload to include all of composers libraries
require "vendor/autoload.php";
//use London time to log errors and warnings
date_default_timezone_set("Europe/London");


/*create a new instance of monolog
$log = new Logger('name');
$log->pushHandler(new StreamHandler('app.txt', Logger::WARNING));
$log->addWarning('Warning: ');*/

$app = new \Slim\Slim();  //new slim object instance

//define a HTTP GET route
$app->get('/hello/:name', function ($name) {
    echo "Hello, $name";
});

//Run the slim application
$app->run();


?>

Thanks

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

My project root, which is a folder called composer has

vendor ---> slim ---> slim ---> Slim, tests

I think it needs a file in the second slim folder called slim.php but i don't have one.

Look a this path: vendor/slim/slim/Slim/Slim.php

Kevin Korte
Kevin Korte
28,149 Points

Where it reads from is vendor -- slim -- slim -- Slim -- Slim.php

Before you get too far into this side of things, try adding the use ($app) to your get as I state in my comment below. I know it will not work without that.

Kevin Korte
Kevin Korte
28,149 Points

What do you have inside of vendor/slim/slim/Slim ?

Here's what I did:

  • Delete the slim framework version 3 manually from the vendor directory.
  • Modify the composer JSON file.
"require": {
        "monolog/monolog": "^1.21",
        "slim/slim": "2.*"
    }
  • Re-install the slim framework version 2 via composer in the command line interface.
composer require slim/slim " 2.* "
  • In my index.php, since the 'Slim/Slim.php' cannot be found (perhaps the directory naming was not defined/configured), I modified the line of code of "require" to:
require 'vendor/slim/slim/Slim/Slim.php';
  • Following the Slim Framework's documentation (for version 2) and to avoid any possible future conflicts, I implemented their "advice" and registered its autoloader as well:
\Slim\Slim::registerAutoloader();
  • Instantiate Slim class:
$app = new \Slim\Slim();
  • Follow along with the rest of the TeamTreehouse tutorial for this section:
$app->get('/hello/:name', function ($name) {
        echo "Hello, $name";
    });

$app->run();

My target page (404) showed up at this point. The " require 'vendor/slim/slim/Slim/Slim.php'; " code instead of " require 'Slim/Slim.php'; " may need further investigation on our local ends though.

Best wishes.

5 Answers

Kevin Korte
Kevin Korte
28,149 Points

Jonathan, if you want to use V3, you'll want to use the V3's docs too.

Your app can't find the slim class because it's location changed between 2 and 3. You're referencing the V2 location. In V3 it's $app = new Slim\App();

You're code might significantly change between the V2 lessons here and the V3 out now. https://github.com/slimphp/slim/tree/3.x

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

I did try to to back to 2.* versions like Hampton was using but Composer woulldn't have it. I didn't know how to set it to download the same versions he was using so in the end I gave in and just went with the defaults.

I'm finding it all very confusing though. It's a shame it's all changed in only 6 months.

Kevin Korte
Kevin Korte
28,149 Points

When you rolled back, did you delete your composer.lock file and than use the install command again?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

I always delete the files from my project folder before installing things again. What I found was it kept looping round asking me to install a package when i tried to type the version number. Did I miss a step that time and not delete the lock file?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

So when I enter the version constraint am I supposed to type *^2. ?

Kevin Korte
Kevin Korte
28,149 Points

I'm not sure, are you doing this from the command line? I haven't taken these courses so I'm not sure how Hampton did it.

Here is what Hampton had in his composer.json "slim/slim": "^2.6" which tells composer to grab slim 2.6 or greater for non-breaking changes. (i.e. 2.7, 2.8) but do not grab 3.0 or greater.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Yea i use MS DOS prompt on windows 7 for composer.

Well.. I'm about ready to give up I'm afraid.

Notice: Undefined variable: log in C:\xampp\htdocs\composer\index.php on line 16

Fatal error: Call to a member function addWarning() on null in C:\xampp\htdocs\composer\index.php on line 16

composer.json

{
    "name": "pc2/composer",
    "description": "First project in composer",
    "authors": [
        {
            "name": "Jonathan Grieve",
            "email": "emailaddress"
        }
    ],
    "require": {
        "monolog/monolog": "^1.17",
        "slim/slim": "2.*"
    }
}
<?php 

//require autoload to include all of composers libraries
require "vendor/autoload.php";

// use London time to log errors and warnings
date_default_timezone_set("Europe/London");

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

//create a new instance of monolog
//$log = new Logger('name');   //new monolog logger instance
//$log->pushHandler(new StreamHandler('app.txt', Logger::WARNING));  //use object object operator

$log->addWarning('Warning: ');

$app = new \Slim\Slim();  //new slim object instance

//define a basic HTTP GET route
$app->get('/hello/:name', function ($name) {
   echo "Hello, $name";
});

//Run the slim application
$app->run();

//echo "Hello, World!";
?>

This is using the 2.* version of Slim too.

Kevin Korte
Kevin Korte
28,149 Points

That error is two things.

  1. $log is never defined. Your $log = new Logger('name'); is commented out.
  2. You're calling addWarning but you've never created an instance of the Logger class, which is the first error.
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Fantastic!

We've got there in the end, thanks for sticking with me over the last day! I can now move forward!

But I'll be investigating how I can go forward with the latest versions in the future. :-)

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Fantastic!

We've got there in the end, thanks for sticking with me over the last day! I can now move forward!

But I'll be investigating how I can go forward with the latest versions in the future. :-)

Kevin Korte
Kevin Korte
28,149 Points

So its working now?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Yes it's working :-)

I've got the 404 page error which is where I left off, I'm about to go forward with the video to remind myself why we get the 404 error :-)

Thanks for all your help.

Kevin Korte
Kevin Korte
28,149 Points

Great! You're welcome. Just a little untangling was all, haha!

Kevin Korte
Kevin Korte
28,149 Points

It is compatible. Have you installed Slim via Composer? I assume you have.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Oh yes, i've even done it twice just to make sure. I did my development on my hard drive then realised I needed to get it onto a server so I did it again on my local host. I've done all the downloading i need to on composer but it doesn't seem to work.

If I take out the require slip code from the documentation (which I realise isn't included in my code) I get this

Fatal error: Class 'Slim\Slim' not found in C:\xampp\htdocs\composer\index.php on line 14
Kevin Korte
Kevin Korte
28,149 Points

Right, because it's not able to find slim.

Make sure you were in the root of your project directory in your command line when you installed Slim.

In your project root, you should have a vendor folder and than a slim folder. This is what composer would have done. Do you have that?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

So it's a compatability issue between me and the video then. I'll have to go back through the composer install then and see if i can get the matching version then.

Why can't it be simple? :p

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

So it's a compatability issue between me and the video then. I'll have to go back through the composer install then and see if i can get the matching version then.

Why can't it be simple? :p

Ah. Your structure should look like this:

C:\xampp\htdocs\root folder\vendor\slim\

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

So C:\xampp\htdocs\root folder\vendor\slim\Slim ?

Either way I'm going to have to look at this again.

I can't believe it'd be all that different from 3.0 compared to 2.6. :-)

Thanks all I#ll check back. :)

Actually, the path to Slim.php would be:

C:\xampp\htdocs\root folder\vendor\slim\slim\Slim\Slim.php

I think you are missing a part of your get line. It should read like this:

<?php

$app->get('/hello/:name', function ($name) use ($app) {
    echo "Hello, $name";
});

If this does not solve your issue, please post your composer.json file.

Kevin Korte
Kevin Korte
28,149 Points

That was going to be my next question, is lets see the composer.json file

no matter what, this will not work without the use ($app) addition to the get statement.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Here's my JSON file.. outside the vendor file, i don't have any others.

{
    "name": "jonnie/jgdm",
    "description": "First Composer package",
    "license": "1",
    "authors": [
        {
            "name": "Jonnie",
            "email": "email"
        }
    ],
    "minimum-stability": "beta",
    "require": {
        "monolog/monolog": "^1.17",
        "slim/slim": "^3.0@RC"
    }
}
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hi Ted,

I've got the code exactly the same as in the video.

Looking at the video, I agree that you do. Sorry.

There is one significant difference, though. You are using Slim 3.0 and the video is written for 2.6. 3.0 has significant changes, although I do not know what they are. I also do not recognize the @RC portion of the require in your JSON file. I have no idea if that is correct or not. You might change back to 2.6 and see if it works.

This is my JSON file for the final project:

{
    "name": "ted/composer_project",
    "description": "Initial project with composer",
    "authors": [
        {
            "name": "shred3590",
            "email": "teds@biblewordstudy.net"
        }
    ],
    "require": {
        "monolog/monolog": "^1.13",
        "slim/slim": "^2.6",
        "twig/twig": "^1.18",
        "slim/views": "^0.1.3",
        "swiftmailer/swiftmailer": "^5.4"
    }
}
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

So it's a compatability issue between me and the video then. I'll have to go back through the composer install then and see if i can get the matching version then.

Why can't it be simple? :p

This is the code that you will ultimately end up with:

<?php
// require Slim
require 'vendor/autoload.php';
// Set time zone
date_default_timezone_set('America/Los_Angeles');

//slim-views is required for Twig
//see https://github.com/slimphp/Slim-Views
//must install via composer
$app = new \Slim\Slim(array(
    'view' => new \Slim\Views\Twig()
));

$view = $app->view();
$view->parserOptions = array(
    'debug' => true
);

//the code below enables us to use the helpers below
//urlFor siteUrl baseUrl currentUrl
//documentation at https://github.com/slimphp/Slim-Views
$view->parserExtensions = array(
    new \Slim\Views\TwigExtension(),
);

$app->get('/', function() use ($app) {
    $app->render("about.twig");
})->name('home');

$app->get('/contact', function() use ($app) {
    $app->render("contact.twig");
})->name('contact');

$app->post('/contact', function() use ($app) {
    $name = $app->request->post('name');
    $email = $app->request->post('email');
    $msg = $app->request->post('msg');

    if (!empty($name) && !empty($email) && !empty($msg)) {
        $cleanName = filter_var($name, FILTER_SANITIZE_STRING);
        $cleanEmail = filter_var($email, FILTER_SANITIZE_EMAIL);
        $cleanMsg = filter_var($msg, FILTER_SANITIZE_STRING);
    } else {
//message the user there was a problem
        $app->redirect('/contact');
    }


    $transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
    $mailer = \Swift_Mailer::newInstance($transport);

    $message = \Swift_Message::newInstance();
    $message->setSubject('Email from Our Website');
    $message->setFrom(array(
        $cleanEmail => $cleanName
    ));

    $message->setTo(array('teds@biblewordstudy.net'));
    $message->setBody($cleanMsg);



    $result = $mailer->send($message);

    if ($result>0) {
        // send a message that says thank you
        $app->redirect('/');
    } else {
        // send a message to the user that the message failed to send
        // log that there was an error
        $app->redirect('/contact');
    }
});

$app->run();
Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

So... video tells me we can just press enter to let composer download the very latest version of Slim which is ^3.0@RC.

But it does seem like there;s just that one missing file that I need that isn't downloading for whatever reason.