-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Tutorial for simple REST API missing file names #2683
Comments
+1 for this.. Spent way to much time trying to understand what to do with the following block of code.
|
Can someone please post some working code in here until the page is updated? I'm considering using phalcon for an immediate project, but without knowing how to get the REST api functionality to work, I'm going to be forced to use another framework instead. |
What trouble do you have using the code in the tutorial? |
The block of code I pasted above is where my trouble started from as I wasn't sure where to put it.
Here's my entire index.php
EDIT
|
You can place the code in index.php, for tutorial purposes it would be fine place it there, however, as long as your application evolves it can be moved out to more files. Take this structure as example: https://github.com/phalcon/dasshy |
Any idea what's causing that Fatal error? |
You have to access one of the routes defined in the example, for instance: /api/robots, if you aren't using nginx or a virtualhost, make sure you have a .htaccess file: https://github.com/phalcon/dasshy/blob/master/public/.htaccess |
Yeaa sorry about that, I figured that out earlier.. it's this error that I cant figure out
Line 52 being |
This line must be fixed: $app->get('/api/robots', function () use ($app) { |
Ah shit, sorry about that.. I was copying & pasting from the tutorial so I'm not quite sure how that happened. thanks |
I am also having a hard time making the tutorial run. [pid 9708] [client 127.0.0.1:51196] PHP Fatal error: Uncaught exception 'Phalcon\Di\Exception' with message 'Service 'db' wasn't found in the dependency injection container' in /var/www/index.php:37\n The missing file names in the tutorial were causing me headache as well. |
Are you injecting the db to the Dependency Injector? On Thu, Sep 17, 2015 at 11:25 AM peteroruba notifications@github.com
|
Looking at the error message I guess I am not. I copied&pasted the tutorial source code to index.php and Robots.php. |
$di = new FactoryDefault(); // Set up the database service // Create and bind the DI to the application |
Thank you, William, for your help. Indeed, I had Now the error is: I had to manually create the table ( create table robots (id INT, name varchar(100)); ) and correct all references in the source code to lower case (working on Linux and wanted to avoid case sensitivity issues) |
Do you have a class like: class robots extends \Phalcon\Mvc\Model
{
} ? |
I have this model delcaration, again, just copied & pasted from the tutorial into models/Robots.php use Phalcon\Mvc\Model; class Robots extends Model (Sorry for the bad formatting) |
Alright, I have everything working now. It was a case sensitivity issue which needed to be fixed a several lines ("Robots" class, "robots" MySQL table name). From a beginner's point of view I would suggest the following updates to the tutorial:
Once again, thank you for your help, William & Andres |
hi,
thrown in C:\new\xampp\htdocs\projects\index.php on line 47 my files: use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Message;
use Phalcon\Mvc\Model\Validator\Uniqueness;
use Phalcon\Mvc\Model\Validator\InclusionIn;
class Robots extends \Phalcon\Mvc\Model
{
public function validation()
{
// Type must be: droid, mechanical or virtual
// Robot name must be unique
$this->validate(
new Uniqueness(
array(
"field" => "name",
"message" => "The robot name must be unique"
)
)
);
// Year cannot be less than zero
if ($this->year < 0) {
$this->appendMessage(new Message("The year cannot be less than zero"));
}
// Check if any messages have been produced
if ($this->validationHasFailed() == true) {
return false;
}
}
} index.php use Phalcon\Loader;
use Phalcon\Mvc\Micro;
use Phalcon\DI\FactoryDefault;
use Phalcon\Db\Adapter\Pdo\Mysql as PdoMysql;
use Phalcon\Http\Response;
// Use Loader() to autoload our model
$loader = new Loader();
$loader->registerDirs(
array(
__DIR__ . '/models/'
)
)->register();
$di = new FactoryDefault();
// Set up the database service
$di->set('db', function () {
return new PdoMysql(
array(
"host" => "localhost",
"username" => "root",
"password" => "",
"dbname" => "api-one"
)
);
});
// Create and bind the DI to the application
$app = new Micro($di);
// Retrieves all robots
$app->get('/api/robots', function () use ($app) {
$phql = "SELECT * FROM robots ORDER BY name";
$robots = $app->modelsManager->executeQuery($phql);
$data = array();
foreach ($robots as $robot) {
$data[] = array(
'id' => $robot->id,
'name' => $robot->name
);
}
echo json_encode($data);
});
$app->notFound(function () use ($app) {
$app->response->setStatusCode(404, "Not Found")->sendHeaders();
echo 'This is crazy, but this page was not found!';
});
$app->handle(); .htaccess <IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
</IfModule> mysql table scheme: Database: CREATE TABLE `robots` (
`id` int(11) NOT NULL,
`name` int(250) NOT NULL,
`type` varchar(250) NOT NULL,
`year` varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1; and i have the rewrite_mod on, any clue what might cause the error?? |
okay i fixed it, turns out the problem is with model path, i just moved models folder to root dir and works fine, now have another error: any help? UPDATE: |
I made the tuto too, and it is not possible to Insert or Update a robot Same error as mutazhameed , getJsonRawBody is always null. Can someone help us? |
This is Phalcon Docs issue |
The tutorial for creating a simple REST API with phalcon is missing the filenames so a beginner cannot know which files to copy/paste the various code bits into. The only mention is for index.php. Otherwise we're left to guess and there's no zip to download the source.
http://docs.phalconphp.com/en/latest/reference/tutorial-rest.html
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: