symfony3 controller file gives a 404 error - symfony-3.1

install symfony3 on a wamp server was fine using composer
On the http://localhost:9080/SymfonyApp/web/
i get the welcome page, thats fine
Created todocontroller.php as shown below in C:\wamp\www\symfonyApp\src\AppBundle\Controller
#
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class TodoController extends Controller
{
/**
* #Route("/todos", name="todo_list")
*/
public function indexAction(Request $request)
{
die('TODOS');
// replace this example code with whatever you need
return $this->render('default/index.html.twig', [
'base_dir' =>
realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
]);
}
}
#
when i go to http://localhost:9080/SymfonyApp/web/todos
I should see TODOS on the browser but instead i get a 404 error
Can anybody please tell me what is wrong with the route?

Related

Apigility Error after installation and creation of first service of demo

i have just installed Apigility and following the tutorials i have an error. When i try with Postman to call my service i get an error like this
Zend\View\Renderer\PhpRenderer::render: Unable to render template "status/v1/rpc/ping/ping/ping"; resolver could not resolve to a file
My call on postman is like this.
http://localhost/demo/api/public/ping
How to solve this problem?
Apigility as part of Zend Framework is now part of the open source Laminas project and is called Laminas API Tools.
Make sure that Zend OPcache is disabled in your PHP configuration before trying to create your API service.
Quick steps to verify:
create a phpconfig.php file that displays php configuration for your development server. Do not put this in production. See for details https://www.php.net/manual/en/function.phpinfo.php
open this file on your server http://localhost:8080/phpconfig.php and look for two things a) ZendOPcache - if it is enabled, then look at b) loaded php.ini something like /etc/php7/cli/php.ini
add opcache.enable=0 to the [opcache] section. Even if it is commented out, it is still loaded, you saw it above right?
restart your PHP server / application to verify that Zend OPcache is off and that's it.
For anyone have this problem, in the example of Apigility change the example code from this:
namespace Status\V1\Rpc\Ping;
use Zend\Mvc\Controller\AbstractActionController;
use ZF\ContentNegotiation\ViewModel;
class PingController extends AbstractActionController
{
public function pingAction()
{
return new ViewModel([
'ack' => time()
]);
}
}
to this
namespace Status\V1\Rpc\Ping;
use Zend\Mvc\Controller\AbstractActionController;
class PingController extends AbstractActionController
{
public function pingAction()
{
return ['ack' => time()];
}
}
Doing this the example is fine.

Hybris + swagger integration swagger-ui.html UnknownResourceError

I'm trying to integrate swagger in MYcommercewebservices.
I read post and done all steps listed on it, but still having this error.
https://localhost:9002/mycommercewebservices/v2/v2/api-docs working fine. https://localhost:9002/mycommercewebservices/v2/swagger-ui.html - return UnknownResourceError.
Furthermore - if I navigate to https://localhost:9002/mycommercewebservices/swagger-ui.html (without 'v2') it'll show me this message (javascript alert):
Unable to infer base URL. This is common when using dynamic servlet
registration or when the API is behind an API Gateway. The base URL is
the root of where all the swagger resources are served. For e.g. if
the API is available at http://example.org/api/v2/api-docs then the
base URL is http://example.org/api/. Please enter the location
manually:
I found this controller, and probably part of the problem was in it because it was throwing an exception when I navigated to https://localhost:9002/mycommercewebservices/v2/swagger-ui.html
#Controller
public class DefaultController
{
#RequestMapping
public void defaultRequest(final HttpServletRequest request)
{
throw new UnknownResourceException("There is no resource for path " + YSanitizer.sanitize(request.getRequestURI()));
}
}
Now I disabled controller, but still having the same exception, but now it's in json format instead of .xml.
Thank you!
The main problem was in DefaultController (in MYcommercewebservices)
#Controller
public class DefaultController
{
#RequestMapping
public void defaultRequest(final HttpServletRequest request)
{
throw new UnknownResourceException("There is no resource for path " + YSanitizer.sanitize(request.getRequestURI()));
}
}
It was catching my request and throwing the exception.
When I disabled this controller, I continued to receive an exception, but now it was in json format(before it was in xml).
Than I added this to springmvc-v2-servlet.xml
<mvc:default-servlet-handler/>
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
Now UI works fine!
Also there were another manipulation before all this, but you can find them in hybris experts(quite big post).

Same Url but different controller after logged in to the application

I want to make simple application using laravel5.2 in which there will come sign in form on base url when i log in to the application there need to give a different view i.e. Client dashboard at same url. How can i do that Please help me. Thanks In Advance !
You could do something like this for root / URL:
Route::get('/', function(){
if (!Auth::check()) {
return View::make('login'); // login view if not authenticated
// or call controller for login
}
else{
return View::make('dashboard'); // dashboard view if authenticated
// or call controller for dashboard
}
});
This is very simple. You have to just make a function in WelcomeController or in other controller. And do check if user is already login or not and redirect to proper view page accordingly.
For example :
In your routes.php file write route like this
$router->get('/',array(
'as' => 'home',
'uses' => 'WelcomeController#welcome'
));
And in your controller ,in case of this example : WelcomeController make a function named welcome and do some auth check like this
public function welcome()
{
if(Auth::check()){
//get some data for user dashboard
return view('dashboard');
}
return view('login');
}
PS: for good practice, use dependency injection. In this case inject Guard
class instead of using Auth facade.

Setting up laravel-oauth2 (facebook login), running into errors

I am trying to set up the oauth2 authentication system (for Facebook specifically) for Laravel 4 seen here: https://github.com/madewithlove/laravel-oauth2
I have a controller, Oauth2Controller.php, that controls this. I am trying to include the the necessary files in the package into my controller. Following the example for that repository, I use:
use OAuth2\OAuth2;
use OAuth2\Token_Access;
use OAuth2\Exception as OAuth2_Exception;
However, when I run the page, it is throwing an error for these lines. Saying:
Symfony \ Component \ Debug \ Exception \ FatalErrorException
syntax error, unexpected 'as' (T_AS), expecting ',' or ';' or '{'
It references directly the third line: use OAuth2\Exception as OAuth2_Exception;
I am not sure why it is throwing this error.
Here is the full code of my controller:
<?php
class Oauth2Controller extends BaseController
{
use OAuth2\OAuth2;
use OAuth2\Token_Access;
use OAuth2\Exception as OAuth2_Exception;
public function getIndex($provider) {
$provider = OAuth2::provider($provider, array(
'id' => '**************',
'secret' => '******************',
));
if(! isset($_GET['code'])) {
return $provider->authorize();
}
else
{
// Howzit?
try
{
$params = $provider->access($_GET['code']);
$token = new Token_Access(array(
'access_token' => $params->access_token
));
$user = $provider->get_user_info($token);
// Here you should use this information to A) look for a user B) help a new user sign up with existing data.
// If you store it all in a cookie and redirect to a registration page this is crazy-simple.
echo "<pre>";
var_dump($user);
}
catch (OAuth2_Exception $e)
{
show_error('That didnt work: '.$e);
}
}
}
}
In my routes, to get to the page, I have:
Route::get('oauth/{provider}', 'Oauth2Controller#getIndex');
THank you for your help and insights!
I have never seen a use clause (in this context, at least) within a controller's definitions.
Try placing the three clauses above class. Those need to import the namespaces for use in your class:
use <class>;
use <class> as {a_class}
...
class <name> ... {

403 Error page not showing in Symfony

I am using Symfony 1.4 for onr of my project and it is needed to show a custom 403 error page. I have given the following in settings.yml
.actions:
error_404_module: errors
error_404_action: error404
error_500_module: errors
error_500_action: error500
error_403_module: errors
error_403_action: error403
I also have the error modules and corresponding error success pages. But it is not showing the custom error pages. Instaed in some cases for ex: 404 it is showing a debug page . 403 page is returned with a default forbidden error message. My action file is like the following:
class errorsActions extends sfActions
{
/**
* Executes index action
*
* #param sfRequest $request A request object
*/
public function executeIndex(sfWebRequest $request)
{
//$this->forward('default', 'module');
}
public function executeError404(sfWebRequest $request)
{
}
public function executeError500(sfWebRequest $request)
{
}
public function executeError403(sfWebRequest $request)
{
}
}
EDIT : Adding settings.yml modifications
.actions:
error_404_module: errors
error_404_action: error404
error_500_module: errors
error_500_action: error500
secure_module: errors
secure_action: error403
In your case the settings.yml should be:
all:
.actions:
# page not found
error_404_module: errors # module "errors"
error_404_action: error404 # action "executeError404"
# 403 credential required
secure_module: errors # module "errors"
secure_action: error403 # action "executeError403"
For the 500 error page, create a directory error in your app/appname/config directory and put a error.html.php file in it (a complete html page).
About the 404 error page, this page (like the 500 error page) is overwritten in the dev environment with the debug page but it will show correctly in the prod environment.
That is not the correct way to override the 403 error pages. Use secure_module and secure_action.
you probably need to unsecure the 404 action:
see here: http://symfony-check.org/permalink/customize-the-oops-page-not-found-page

Resources