Use normal PHP-routes (&) with symfony routing (/) for fullcalendar.io - php

i want to use fullcalendar.io with my Symfony2 project.
The Fullcalendar sends such requests for getting Event data:
/myfeed.php?start=2013-12-01&end=2014-01-12&_=1386054751381
Symfony2 usally routes with Slashes in the URl, it only exists this route:
/myfeed/start=2013-12-01/end=2014-01-12/_=1386054751381
How can I use the Feature of Fullcalendar for Symfony2?
Here my Test-Controller:
<?php
namespace Chris\KfzBuchungBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
class AjaxGetWeekController extends Controller
{
public function indexAction(Request $request, $start)
{
$response = new Response(json_encode(array("title" => 'blub',"start" =>$start)));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
}
Thank you!

Even though Symfony routes are typically set up differently, you can still very easily retrieve the query string parameters. See the following:
http://symfony.com/doc/current/quick_tour/the_controller.html#getting-information-from-the-request
So in your controller all you have to do is:
$start = $request->query->get('start');
$end = $request->query->get('start');
$timestamp = $request->query->get('_');
I encourage you to read the Symfony documentation as it is very thorough with many examples. Also this question has been answered already on StackOverflow with many more examples here: How to get the request parameters in symfony2

Related

Botman laravel REST API setup

Im new to Botman.
I try to implement a simple function to test how it works, but I keep getting empty response, it look like botman do not hears my message.
I installed botman without studio since, I'm trying to keep things simple. I also installed a webdriver as it says in documentation.
In my project I use JWT as an authentification, so i created a protected route like this:
Route::group(['middleware' => ['assign.guard:user', 'jwt.auth']], function () {
Route::post(
'/',
'UserBotManController#startConversation'
)->name('botman.user.start');
});
The controller looks like this:
<?php
namespace Project\UI\Api\Controllers\User\Botman;
use App\Http\Controllers\Controller;
use BotMan\BotMan\BotMan;
use BotMan\BotMan\BotManFactory;
use BotMan\BotMan\Drivers\DriverManager;
class UserBotManController extends Controller
{
public function startConversation()
{
$config = [];
DriverManager::loadDriver(\BotMan\Drivers\Web\WebDriver::class);
$botman = BotManFactory::create($config);
$botman->hears('hello', function (BotMan $bot) {
$bot->reply('Hello yourself.');
});
$botman->listen();
}
}
No when I send a request to this route a get empty response:
Looks like botman can't hear my message...
I try to looked inside with: dd($botman->getDriver());
And I see that the content has all the data:
Can any one help me to understand, how I can make it work?
Ok, so finally I found a solution. I have checked what request it sends from https://botman.io website and it is Form Data, not JSON.
diver field must be set to web!
Hope it will help someone.

Laravel Api routes fail to load. 404 not found

For reference I have been working with this tutorial https://scotch.io/tutorials/build-a-time-tracker-with-laravel-5-and-angularjs-part-2.
I wanted to become more familiar with laravel 5 since I had previously only used 4 and found the above tutorial which also mixed in a little angular js. I followed part one and two of the tutorials to the letter and set up a database using mysql and phpmyadmin like directed.
I get to a section about halfway through which sets up a group route with the prefix api to pull seeded data from the database and display it in the view.
// app/Http/routes.php
...
// A route group allows us to have a prefix, in this case api
Route::group(array('prefix' => 'api'), function()
{
Route::resource('time', 'TimeEntriesController');
Route::resource('users', 'UsersController');
});
After this point I go to the page and the area that was previously rendered with data from a file instead of a database is now blank. If I inspect the element I get "failed to load resource the server responded with a status of 404 (not found)" and it displays my path time-tracker-2/public/api/time.
The routes work with these two controllers to populate the page with userdata from my database
// app/Http/Controllers/TimeEntriesController.php
...
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\TimeEntry;
use Illuminate\Support\Facades\Request;
class TimeEntriesController extends Controller {
// Gets time entries and eager loads their associated users
public function index()
{
$time = TimeEntry::with('user')->get();
return $time;
}
// app/Http/Controllers/UsersController.php
...
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;
class UsersController extends Controller {
// Gets all users in the users table and returns them
public function index()
{
$users = User::all();
return $users;
}
Again I haven't worked much with Laravel and this is my first time messing with angular so I don't know if I am missing something super obvious or what the deal is. I have checked over all my code and compared it to the sample code and they are identical other than my database information. I have also scrapped the project and started from scratch and still get the same error when I get to this point.
Any sort of direction to look would be greatly appreciated because this error is driving me nuts.
Remember to point on the public folder, not on the root folder. That's why your URL is time-tracker-2/public/api/time and not time-tracker-2/api/time. This should fix your 404 error.

Making my own argument resolver for my my own PHP framework

I decided to make my own mini - framework for PHP that I am going to use for a real life job, creation of a Web Service for a social app.
I got started with Fabien Potencier's guide to creating your own framework on top of the Symfony's components - http://symfony.com/doc/current/create_framework/index.html.
I really liked his classLoader and http-foundation libraries and decided to integrate them.
I read the whole tutorial but I decided to stop integrating Symfony's components up to part 5 of the tutorial where he gets to the Symfony http kernel, route matcher and controller resolver (excluding those).
There are the front controller and route mapper file of my framework that qre in question.
front.php(the front controller)
<?php
require 'config/config.php';
require 'config/routing.php';
require 'src/autoloader.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$request = Request::createFromGlobals();
$response = new Response();
$path = $request->getPathInfo();
if(isset($siteMap[$path])) {
call_user_func_array('src\Controllers' .'\\' . $siteMap[$path]['controller'] . '::' . $siteMap[$path]['action'], array($siteMap[$path]['arguments']));
} else {
$response->setStatusCode('404');
$response->setContent('Page not found');
}
$response->send();
?>
My routing file :
/*
Map of routes
*/
$siteMap = array('/' => array('controller' => 'IndexController', 'action' => 'indexAction', 'arguments' => ''),
'/categories' => array('controller' => 'CategoriesController', 'action' => 'indexAction', '
What I want to know now is without further using Symfony's components, how can I do something like this:
In my routing file I wanna add an URL like '/hello' with Controller - Hello Controller and arguments name,age,gender that would correspond to a request in the browser GET www.base/hello/samuel/11/male.
And in the HelloController have an indexAction($name, $age, $gender) { ... }. I have tried looking at Symfony's source but it eludes me so far(I spent a lot of time going trough the source code of the libraries). I am going to modularize and separate the functions of the front controller and controller resolving even further but I want to get this down.
Ah and also any advice on building my framework further would be welcome(I am creating a framework for a REST - like web service that needs to be scalable and fast and handle tens of thousands requests per second maybe).
You must define regexes for your routes and match them against the requests, if you want to know how Symfony transforms its own route format e.g: '/path/to/{id}' to a regex, see RouteCompiler::compilePattern. That's out of the scope of this question and I will provide no code for it.
The regex for your example route will be something like ^/hello/(\w*)/(\d*)/(\w*)/$, this will match '/hello/any string/any number/any string/' e.g: /hello/samuel/11/male (don't forget the ^ and the $, they match the beginning and end of the string).
You must provide a regex for each route, and do a preg_match(rawurldecode($request->getPathInfo(), $regex /* as created earlier*/, $matches) against all of the routes (until one matches), instead of the isset($sitemap[$path]) if this returns false, the route does not match.otherwise you must get the arguments for your controller like so:
preg_match(rawurldecode($request->getPathInfo(), $regex /* as created earlier*/, $matches)
$args = array();
// we omit the first element of $matches, since it's the full string for the match
for($i = 1; $i < count($matches); $i++){
$args[] = $matches[$i];
}
Now you can merge $args with the default arguments and call the controller.
See UrlMatcher::matchCollection() on how Symfony matches each route against the regex, and constructs the arguments.
I would like to add some additional information. In Symfony you can get the Arguments of your request into the method arguments:
public function fooAction($bar);
In this case it would look in the $request->attributes for a key with bar. if the value is present it will inject this value into your action. In Symfony 3.0 and lower, this is done via the ControllerResolver::getArguments(). As of Symfony 3.1 you will be able to implement or extend an ArgumentResolver.
Utilizing the ArgumentResolver means you can easily add your own functionality to the resolving. The existing value resolvers are located here.

Symfony2 tutorial deprecated function

I am learning Symfony2 Framework, and to start things of I found this tutorial: Tutorial. Everything has been going well until I hit this issue:
in Tutorial Part2. In Creating the form in the controller section i discover that the getRequest() function is deprecated and bindRequest() is not found in class.
These two have held me back with the tutorial and learning progress. I there any other way of constructing this controller without using these functions or are there other functions that do exactly the same thing.
See this part of the Symfony Documentation. It shows that you should use handleRequest, like so:
// Top of page:
use Symfony\Component\HttpFoundation\Request;
...
// controller action
public function newAction(Request $request)
{
$form = $this->createFormBuilder()
// ...
->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
// perform some action...
return $this->redirect($this->generateUrl('task_success'));
}
return $this->render('AcmeTaskBundle:Default:new.html.twig', array(
'form' => $form->createView(),
));
}
You may also find this link useful: Handling Form Submissions.
The Request as a Controller Argument
Getting the request object in this way might be a little confusing at first, to quote the documentation:
What if you need to read query parameters, grab a request header or
get access to an uploaded file? All of that information is stored in
Symfony's Request object. To get it in your controller, just add it as
an argument and type-hint it with the Request class:
use Symfony\Component\HttpFoundation\Request;
public function indexAction($firstName, $lastName, Request $request)
{
$page = $request->query->get('page', 1);
// ...
}
This information can be found on the Controller Documentation.

Using Symfony with Backbone.js

I wanted to get views from the symfony community about how one would achieve using backbone.js to create a one paged application.
Is it even possible to call the functions in symfony (e.g public function executeCreate()) through backbone.js) has anyone done it?
Can you give me some resources? thanks in advance.
Why not? Symfony easily can return JSON data on request by backbone clientside app. For Symfony2 it can be like this:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class MyController extends Controller {
public function createAction($requestParam1, $requestParam2)
$response = new Response(json_encode(array(
'requestParam1' => $requestParam1,
'requestParam2' => $requestParam2
)));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
}
I would recommend you to have a look at the FOSRestBundle. Another attempt could be to handle the requests yourself, and return the date from the orm/odm via Serializer
the only thing bothered me so far, was the form handling (used backbone-forms), where I simply not was able to keep my code DRY (had duplicate data for the form+validation in symfony and in the frontend-part).
If you want to start a new Symfony2 + backbone app, I would recommend this bundle:
https://github.com/gigo6000/DevtimeBackboneBundle
There's also a demo app that can be helpful to see the backbone.js and Symfony2 interaction:
https://github.com/gigo6000/DevtimeRafflerBundle

Categories