Symfony2: Weird route not found error - php

So I have it defined as:
/**
* Class TemplateController
* #package TemplateManager\Bundle\DocumentGeneratorBundle\Controller\API
* #Route("/api/v1/templates")
*/
class TemplateController extends Controller
{
/**
* #Route("?available={id}")
* #Method({"GET"})
*/
public function findAllAction($id)
{
echo "Yu";
}
/**
* #Route("/{id}")
* #Method({"GET"})
* #param $id
* #param $template_name
* #return Response
*/
public function findAction($id)
{}
}
When accessing it as: http://localhost/api//v1/templates?available=1 it says no matching route found. Where am I doing wrong? The other route works fine.

Your base route for your controller is defined as #Route("/api/v1/templates"), but you're only calling "/api/templates/*". You simply forgot the "/v1/" in there.
Try calling http://localhost/api/v1/templates?available={id}.
Please note: on the CLI you can always dump all registered routes for easier debugging. Just type:
$> app/console debug:router
# or bin/console if you're using Symfony3 and above
$> bin/console debug:router

Shouldn't it be
http://localhost/api/template?available=1
instead of
http://localhost/api/templates?available=1
check your controller class and word used in the URL.

Related

command not being found even though I am registering it Laravel 6.x

So consider the service provider, yes I know I am registering this command in two places, but just give me a moment to explain:
<?php
namespace App\Modules\Core\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Foundation\AliasLoader;
use App\Modules\Core\Handlers\RedirectHandler;
use App\Modules\Core\Console\Commands\CreateAdminUser;
use App\Modules\Core\Values\IsMedicalRecordEmpty;
class CoreProvider extends ServiceProvider
{
protected $commands = [
CreateAdminUser::class,
];
/**
* Register services.
*
* #return void
*/
public function register()
{
$this->app->bind(RedirectHandler::class, function($app) {
return new RedirectHandler();
});
$this->app->bind(IsMedicalRecordEmpty::class, function($app) {
return new IsMedicalRecordEmpty();
});
}
public function register() {
$this->commands($this->commands);
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->commands([
CreateAdminUser::class,
]);
}
}
}
So as stated before we can see that I am registering this command in two places, because I am trying to figure out why calling php artisan doesn't show the command, it only shows if I register it in the app\Console\Kernel, but because I am trying to take a modular approach to the code base, I want to register it in my service provider, to which is registered as such:
'providers' => [
...
/**
* Module Related Providers
*/
App\Modules\Core\Providers\CoreProvider::class,
...
],
I register the provider properly, I (yes I know I don't need to register the command twice) register the command in the way that stack has explained it, either way should in theory work.
But alas the command does not show up when I run php artisan. At all.
The command is simple:
<?php
namespace App\Modules\Core\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use Illuminate\Auth\Events\Verified;
use App\Modules\Core\Users\Mail\GeneratedAdmin;
use App\Modules\Core\Users\Models\User;
use App\Modules\Core\Users\Services\RegisterUserService;
class CreateAdminUser extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'create:admin {first_name} {last_name} {email}';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Create one admin.';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
// do stuff here ...
}
}
Any ideas?
Your code is redeclaring register(), first in the method with the binds, and then again with your method calling $this->command() - are you even referencing your service provider correctly? PHP should have told you this - it did for me when I tried your sample code...
Whoops\Exception\ErrorException : Cannot redeclare App\Modules\Core\Providers\CoreProvider::register()
It's worth noting that removing the first method with the binds caused the command to show for me.

Infinite HTTP 301 redirect loop inside Symfony 3.4 application when creating a new blank slash (/) route

I am having a hard time understanding why I am hitting an infinite HTTP 301 redirect loop inside my Symfony 3.4.20 application when I build a route like this:
/**
* #Route("/temp-crmpicco")
*/
class TempController extends Controller
{
/**
* #Route(
* "/",
* name="temp_crmpicco_index"
* )
*
* #param Request $request
*/
public function indexAction(Request $request)
{
return $this->render('#App/CRMPicco/index.html.twig', []);
}
}
However, when I define the route as the following it works.
* #Route(
* "/crm",
* name="temp_crmpicco_index"
* )
The redirects look like this in the console:
$ bin/console --env=dev debug:route | grep crmpicco
temp_crmpicco_index ANY ANY ANY /{_locale}/temp-crmpicco/
Why does the route only work when I give it a random value? Surely it can be just /.

Laravel : artisan command not generating complete auto generated code

I started to learn through a tutorial for learning Laravel 5.
https://tutorials.kode-blog.com/laravel-hello-world
I followed the steps
when I run the command : php artisan make:controller Hello
I am getting just the base code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class Hello extends Controller
{
//
}
However the tutorial specify the auto-generated code with some functions as well inside the class.
It says the auto-generated code is,
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class Hello extends Controller
{
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param Request $request
* #return Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param Request $request
* #param int $id
* #return Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
//
}
}
I could not figure out what configuration changes need to be updated or I am missing something very basic for this framework to start with, I tried to reinstall the application again and the same issue happened again.
That tutorial looks a little dated. In Laravel 5.2, the command was updated to generate a plain controller by default.
To generate a "resource" controller, as shown in the tutorial, you now need to pass the "--resource" flag:
php artisan make:controller Hello --resource
the Artisan command you are looking for is:
php artisan make:controller UserController --resource
You could find all information about Controllers in the official documentation:
Laravel Controller documentation
Or Google for information but specify your Laravel version

laravel 4 restful controller wont work

I am trying to make the laravel 4 controller in mac terminal by
php artisan controller:make UserController
It work's and insert the controller in the folder.
In my route.php i add:
Route::controller('users', 'UserController');
In my UserController in index i make
return "Hello world"
But when i am entering localhost/users it don't show anything, either in /users/create.
What can i do?
Trace errors:
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
open: /Applications/XAMPP/xamppfiles/htdocs/salety/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php
* #param Exception $e
* #return void
*/
protected function handleRoutingException(\Exception $e)
{
if ($e instanceof ResourceNotFoundException)
{
throw new NotFoundHttpException($e->getMessage());
}
UserController
<?php
class UserController extends \BaseController {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
return "Hello world!";
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store()
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param int $id
* #return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
//
}
}
You need to change Index to getIndex when using RESTful controllers.
What you've created using the artisan command is a resource controller.
To get this to work, change your routes.php file to this:
Route::resource('users', 'UserController');
This will make the /users route a resource and allow it to respond properly.
Be sure to look at the documentation on resource controllers and be sure to pay attention to the Actions Handled By Resource Controller section, as this gives you the key to what methods are used for which URI's.
Well for restfull controllers you need to use this form getIndex , getCreate , postRegister..etc , you can either use Route::controller() or Route::resource()
after changing stuff in your routes.php you need to run
php composer dump-autoload
to refresh the autoloading files with edited routes.

How can I have optional parameters in Symfony2 route?

I have this code below:
/**
* Lists all User entities.
*
* #Route("/{cid}",defaults={"cid" = null},name="user")
* #Template()
*/
public function indexAction($cid=null)
{}
Now if I type site/user/1 then it works, but if I type site/user/ it says:
No route found
How can I have it that both routes work?
Try to go to site/user (notice no backslash at the end).
Generally it should work, I have relatively similar configuration working.
But if all else fails you can always define multiple routes for same action, i.e.
/**
* Lists all User entities.
*
* #Route("/", name="user_no_cid")
* #Route("/{cid}", name="user")
* #Template()
*/
public function indexAction($cid=null)
{
Use a yml file for your routing configuration, and add a default value for id in your routing parameters like this:
user:
pattern: /site/user/{id}
defaults: { _controller: YourBundle:Default:index, id: 1 }
See documentation here
You could also do it with a GET parameter, e.g.
/**
* #param Request $request
*
* #return Response
*/
public function displayDetailAction(Request $request) : Response
{
if ($courseId = $request->query->get('courseId')) {

Categories