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 trialJonathan Grieve
Treehouse Moderator 91,253 PointsIs 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
Ted Sumner
Courses Plus Student 17,967 PointsLook a this path: vendor/slim/slim/Slim/Slim.php
Kevin Korte
28,149 PointsWhere it reads from is vendor -- slim -- slim -- Slim -- Slim.php
Jonathan Grieve
Treehouse Moderator 91,253 Pointsthere's no slim.php :/
Ted Sumner
Courses Plus Student 17,967 PointsBefore 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
28,149 PointsWhat do you have inside of vendor/slim/slim/Slim ?
kucxtground
1,034 PointsHere'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
28,149 PointsJonathan, 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
Treehouse Moderator 91,253 PointsI 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
28,149 PointsWhen you rolled back, did you delete your composer.lock file and than use the install command again?
Jonathan Grieve
Treehouse Moderator 91,253 PointsI 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
Treehouse Moderator 91,253 PointsSo when I enter the version constraint am I supposed to type *^2. ?
Kevin Korte
28,149 PointsI'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
Treehouse Moderator 91,253 PointsYea 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
28,149 PointsThat error is two things.
- $log is never defined. Your
$log = new Logger('name');
is commented out. - You're calling
addWarning
but you've never created an instance of the Logger class, which is the first error.
Jonathan Grieve
Treehouse Moderator 91,253 PointsFantastic!
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
Treehouse Moderator 91,253 PointsFantastic!
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
28,149 PointsSo its working now?
Jonathan Grieve
Treehouse Moderator 91,253 PointsYes 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
28,149 PointsGreat! You're welcome. Just a little untangling was all, haha!
Kevin Korte
28,149 PointsIt is compatible. Have you installed Slim via Composer? I assume you have.
Jonathan Grieve
Treehouse Moderator 91,253 PointsOh 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
28,149 PointsRight, 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
Treehouse Moderator 91,253 PointsSo 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
Treehouse Moderator 91,253 PointsSo 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
Ted Sumner
Courses Plus Student 17,967 PointsAh. Your structure should look like this:
C:\xampp\htdocs\root folder\vendor\slim\
Jonathan Grieve
Treehouse Moderator 91,253 PointsSo 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. :)
Ted Sumner
Courses Plus Student 17,967 PointsActually, the path to Slim.php would be:
C:\xampp\htdocs\root folder\vendor\slim\slim\Slim\Slim.php
Ted Sumner
Courses Plus Student 17,967 PointsI 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";
});
Ted Sumner
Courses Plus Student 17,967 PointsIf this does not solve your issue, please post your composer.json file.
Kevin Korte
28,149 PointsThat was going to be my next question, is lets see the composer.json file
Ted Sumner
Courses Plus Student 17,967 Pointsno matter what, this will not work without the use ($app)
addition to the get statement.
Jonathan Grieve
Treehouse Moderator 91,253 PointsHere'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
Treehouse Moderator 91,253 PointsHi Ted,
I've got the code exactly the same as in the video.
Ted Sumner
Courses Plus Student 17,967 PointsLooking 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.
Ted Sumner
Courses Plus Student 17,967 PointsThis 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
Treehouse Moderator 91,253 PointsSo 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
Ted Sumner
Courses Plus Student 17,967 PointsThis 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
Treehouse Moderator 91,253 PointsSo... 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.
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsMy 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.