Why Fatfreeframework (F3) keep give me "HTTP 405 Method Not Allowed" message, even when I have correct Class->method to process the POST|GET request? - fat-free-framework

I've been trying to solve this strange problem. I just want to redirect a request to do a specific function using POST|GET method.
At first, I thought this was a straightforward scenario, but I found it little bit challenging.
I have this in my routing configuration (for GET method) :
[routes]
GET /pub/adsprocess/#command/#adsid=AdsController->processAds
and the code in my "view" file :
< a href="/pub/adsprocess/yes/1" >YES </a>
< a href="/pub/adsprocess/no/2" >NO </a>
My controller look like this :
class AdsController extends Controller
{
function processAds()
{
$command = $this->f3->get('PARAMS.command');
$ads_id = $this->f3->get('PARAMS.adsid');
/*some more process here...*/
}
}
As far as I see, everything looks ok to me. But, all of this doesn't work, F3 keep showing me "Method Not Allowed HTTP 405" error message. And this error message stay there when I use POST method, too.
I'm not sure where to look. Any clue or alternative approach to this problem will highly appreciated.
Thanks,
Sigit

as i can't comment, can we see the autoloader variable? I mean, if you place the AdsController in a Controller Folder and you didn't add the controller folder to the autoloader, then you need to access it like this:
\Controller\AdsController->processAds
and the AdsController.php file must be added the namespace to works:
Namespace Controller;
class AdsController extends \Controller
{
function processAds()
{
$command = $this->f3->get('PARAMS.command');
$ads_id = $this->f3->get('PARAMS.adsid');
/*some more process here...*/
}
}
if you did add it to the autoloader variable, it's should be work on your way.

Related

CakePHP static name of controller

I'm probably missing something really obvious here, but is there a function in CakePHP (I'm on 3.8) that returns the name of a controller without creating an instance of the class?
An instanced controller can call this function:
echo $this->name;
But what I'd like to be able to do, is avoid typing the controller name as a string in, say, an HTML->link(); ie a static call something like:
echo $this->Html->link(
'Dashboard',
['controller' => DashboardsController::name, 'action' => 'index']
);
The reason is that I'm refactoring a couple of controllers and am having to find and replace all of those strings by hand. I come from a .Net background and CakePHP is pretty new to me, so if there's a better (more cakeish) way to carry out the refactoring than the question I'm asking, then I'd be really glad to hear it.
Nothing in the documents is leaping out at me, but I've a feeling there should be a simple answer.
The namespace of a class can be retrieved using ::class property. Checkout the following example:
DashboardsController::class // Cake/Controllers/DashboardController
The name without the namespace can be retrieved with ReflectionClass:
$function = new \ReflectionClass(DashboardsController::class);
var_dump($function->inNamespace());
var_dump($function->getShortName());
Shortname can be used to get the class without namespace:
namespace App;
class Test {
public static function name(){
$function = new \ReflectionClass(self::class);
return $function->getShortName();
}
}
var_dump(Test::name());
Checkout the docs: https://www.php.net/manual/en/language.oop5.constants.php#example-186
Reflection: https://www.php.net/manual/en/reflectionclass.getname.php

Symfony - Unable to load a listener service

I'm trying to load a listener but symfony keeps throwing this error:
ClassNotFoundException in appDevDebugProjectContainer.php line 1783:
Attempted to load class "CommandeListener" from namespace "Louvre\ReversationBundle\Services\Listeners".
Did you forget a "use" statement for another namespace?
My class is properly writen (i guess):
<?php
namespace Louvre\ReservationBundle\Services\Listeners;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
class CommandeListener {
public function checkCommand(FilterResponseEvent $event){
if (!$event->isMasterRequest() && $event->getRequest()->get('_route') == "louvre_reservation_step1") {
return;
}
$url = $this->router->generate("louvre_reservation_step1");
$response = new RedirectResponse($url);
$event->setResponse($response);
}
}
Its path is
src/Louvre/ReservationBundle/Services/Listeners/CommandeListener.php
and here is the yaml service
louvre_reservation.commande.listener:
class: Louvre\ReversationBundle\Services\Listeners\CommandeListener
tags:
- { name: kernel.event_listener, event: kernel.request, method: checkCommande }
I have other services which work perfectly.
they are declared the same way.
I googled the problem but could not find an answer.
Does anyone already faced this problem?
Thanks in advance
It looks like you made a typo in your class include. You wrote "Reversation" instead of "Reservation" which make your code looks into the "Reversation" folder which does not exists.

How to custom Error Pages in Kohana 3.0

I've been trying to find a full explanation on how to custom error pages in Kohana 3.0 and I haven't been lucky so far. So, based on the solution provided by Kohana Guide can anyone show me how to do it?
When I say full explanation I mean, the location of the classes, their names, which ones to extend, full code, and please, a view for one of the errors.
Many thanks.
I suggest you read http://kohanaframework.org/3.0/guide/kohana/conventions. You can work out the files that need to be created for yourself based on the class names and how Kohana autoloads. The beauty of Kohana for me is that it doesn't try and do everything for you and for that reason its is really important to read the documentation in my opinion.
Note: I haven't used Kohana 3.0 in particular but this should apply.
For example, in part 1, is this code:
<?php defined('SYSPATH') or die('No direct access');
class HTTP_Response_Exception extends Kohana_Exception {}
Kohana uses a 'cascading filesystem' so you would add the code shown in 2 to
application/classes/kohana/exception.php
Inside that file you would extend the Kohana exception handler
class Kohana_Exception extends Kohana_Kohana_Exception {
public static function exception_handler(Exception $e)
{
...
}
}
The route shown gets added to your applications bootstrap which is under
application/bootstrap.php
As errors are being routed you can tell what the controller will be:
Route::set('error', 'error/<action>(/<message>)', array('action' => '[0-9]++', 'message' => '.+'))
->defaults(array(
'controller' => 'error_handler'
));
So there will be a controller created at:
application/classes/controller/error_handler.php
This will look like:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Error_Handler extends Controller_Template {
...
public function action_404()
{
$this->template->title = '404 Not Found';
// A view example
$view = View::factory('error/404');
$view->render();
...
}
}
Your views would then (possibly) be placed under:
application/views/error/404.php
The reason the documentation is brief is because the same answers do not apply to everyone. For example the majority of people (I know) use their own templates etc.

symfony 1.4: How to pass exception message to error.html.php?

I tried using special variable $message described here http://www.symfony-project.org/cookbook/1_2/en/error_templates but it seems this variable isn't defined in symfony 1.4, at least it doesn't contain message passed to exception this way throw new sfException('some message')
Do you know other way to pass this message to error.html.php ?
You'll need to do some custom error handling. We implemented a forward to a custom symfony action ourselves. Be cautious though, this action itself could be triggering an exception too, you need to take that into account.
The following might be a good start. First add a listener for the event, a good place would be ProjectConfiguration.class.php:
$this->dispatcher->connect('application.throw_exception', array('MyClass', 'handleException'));
Using the event handler might suffice for what you want to do with the exception, for example if you just want to mail a stack trace to the admin. We wanted to forward to a custom action to display and process a feedback form. Our event handler looked something like this:
class MyClass {
public static function handleException(sfEvent $event) {
$moduleName = sfConfig::get('sf_error_500_module', 'error');
$actionName = sfConfig::get('sf_error_500_action', 'error500');
sfContext::getInstance()->getRequest()->addRequestParameters(array('exception' => $event->getSubject()));
$event->setReturnValue(true);
sfContext::getInstance()->getController()->forward($moduleName, $actionName);
}
}
You can now configure the module and action to forward to on an exception in settings.yml
all:
.actions:
error_500_module: error
error_500_action: error500
In the action itself you can now do whatever you want with the exception, eg. display the feedback form to contact the administrator. You can get the exception itself by using $request->getParameter('exception')
I think I found a much simpler answer. On Symfony 1.4 $message is indeed not defined, but $exception is (it contains the exception object).
So just echo $exception->message.
Et voilĂ !
I've found another trick to do that - sfContext can be used to pass exception message to error.html.php but custom function have to be used to throw exception. For example:
class myToolkit {
public static function throwException($message)
{
sfContext::getInstance()->set('error_msg', $message);
throw new sfException($message);
}
than insted of using throw new sfException('some message') you should use myToolkit::throwException('some message')
To display message in error.html.php use <?php echo sfContext::getInstance()->get('error_msg') ?>

creating functions in CodeIgniter controllers

I have a CodeIgniter application, but one of my controllers must call a data processing function that I have also written myself. The only problem is I can't seem to figure out how to do this. Looking through the user guide it seems that I should put my function inside the class declaration, and prefix it with an underscore (_) so that it cannot be called via the url. However, this is not working. Here's an example of what I mean:
<?php
class Listing extends Controller
{
function index()
{
$data = "hello";
$outputdata['string'] = _dprocess($data);
$this->load->view('view',$outputdata);
}
function _dprocess($d)
{
$output = "prefix - ".$d." - suffix";
return $output
}
}
?>
The page keeps telling me I have a call to an undefined function _dprocess()
How do I call my own functions?
Thanks!
Mala
Edit:
I've gotten it to work by placing the function outside of the class declaration. Is this the correct way of doing it?
This line is creating problem for you:
$outputdata['string'] = _dprocess($data);
Replace with:
$outputdata['string'] = $this->_dprocess($data);