Symfony 2.8 generate route 404 not found error - symfony-2.8

I have created new route for debugging.
created /src/BundleName/Resources/config/routes/default.yml
test_index:
path: /testroutes
defaults: { _controller: BundleName:Test:index }
methods: [GET, POST]
courseflow_backbone_testingroute:
resource: "#BundleName/Resources/config/routes/testing_route.yml"
prefix: /testroute
=====================
created /src/BundleName/Resources/config/routes/testing_route.yml
demo_test:
path: /demotestthing
defaults: { _controller: BundleName:Test:demoTestThing }
methods: [GET, POST]
==================
created /src/BundleName/Controller/TestController.php
namespace BundleName\Controller;
use Symfony\Component\HttpFoundation\Request;
class TestController extends Controller
{
public function indexAction(Request $request)
{
echo "1";die;
}
public function demoTestThingAction(){
echo "12";die;
}
}
=====================
And I have tried to access
http://example.com/testroute/demotestthing
but its getting 404 not found error.
Can you please help me on that.enter code here
Thanks in advance.

Related

User not logged in, even after `LoginSuccessEvent` fired

It worked a while back, but I'm having issues backtracking where I went wrong.
I'll give you all the details first:
#security.yaml
security:
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
providers:
users:
entity:
class: 'App\Entity\User'
property: 'username'
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
lazy: true
provider: users
form_login:
login_path: app_login
check_path: app_login
enable_csrf: true
logout:
path: app_logout
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
- { path: ^/profile, roles: ROLE_USER }
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH ]
Pretty basic, here's where it gets messy for me:
controllers:
resource: ../src/Controller/
type: annotation
prefix: /{_locale}
requirements:
_locale: '%app.supported_locales%'
defaults:
_locales: '%app.default_locale%'
middleware: 'locale_middleware'
app_default_locale_redirect:
path: /
methods: GET
controller: App\Controller\DefaultController::defaultLocaleRedirect
defaults:
_locale: '%app.default_locale%'
requirements:
_locale: '^(?!%app.supported_locales%).*'
app_login:
path: /{_locale}/login
app_logout:
path: /{_locale}/logout
methods: GET
I want to redirect all URLs that don't have a supported locale in the URL to the default locale. That's what the middleware is for, and thats why app_login and app_logout both have /{_locale} in their paths. It works as far as I can see.
My default controller handles that using method:
/**
* #Route("/default-locale-redirect")
*/
public function defaultLocaleRedirect(Request $request): RedirectResponse
{
$defaultLocale = $request->getLocale();
return new RedirectResponse($request->getBaseUrl() . '/' . $defaultLocale . '/');
}
I'm also subscribed to the LoginSuccessEvent event, and relevant events:
public static function getSubscribedEvents(): array {
return [
LogoutEvent::class => 'onLogout',
LoginSuccessEvent::class => "onLogin",
LoginFailureEvent::class => "onLoginFailure"
];
}
public function onLogout(LogoutEvent $event): void {
$this->flash->add("success", $this->trans->trans("account.auth.logged_off"));
}
public function onLogin(LoginSuccessEvent $event): void {
$token = $this->tokenStorage->getToken();
$user = $token->getUser();
$roles = $token->getRoleNames();
$this->flash->add("success", $this->trans->trans("account.auth.logged_on"));
}
public function onLoginFailure(LoginFailureEvent $event): void {
$this->flash->add("error", $this->trans->trans("account.auth.failed"));
}
When I place a breakpoint in onLogin, the breakpoint gets hit, and $token, $user and $roles are populated perfectly.
After the page reloads, though, I notice the user is not authenticated. Somehow I believe the token is not persisted. The dev toolbar confirms this, and is_granted("IS_AUTHENTICATED_FULLY") also returns false.
I tried clearing all caches. What am I doing wrong?

Controller not working (except WeatherForecast) in .NET Core with React

I created a fresh .NET Core with React.js project, which has this by default in Program.cs:
app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
app.MapFallbackToFile("index.html"); ;
The provided WeatherForecastController works, i.e. this works:
https://localhost:44449/weatherforecast
But if I copy WeatherForecastController.cs and name it MyNewController.cs like this, it won't work.
[ApiController]
[Route("[controller]")]
public class MyNewController : ControllerBase
{
...
https://localhost:44449/mynew returns the content of index.html.
What am I missing?
In setupProxy.js you have to add /mynew in the context:
const context = [
"/weatherforecast",
"/mynew",
];
module.exports = function(app) {
const appProxy = createProxyMiddleware(context, {
target: target,
secure: false,
headers: {
Connection: 'Keep-Alive'
}
});
app.use(appProxy);
};
Otherwise, the framework will interpret the url /mynew as a route to a screen and will handle it as such.

Laravel + Vue.js - How to pass data from Controller to vue

I am wanting to pass the data from my app controller to my vue:
class AppController extends Controller
{
/**
* #param Request $request
* #return Response
*/
public function index(Request $request)
{
return response()
->view('pages.app', ['highScore' => HighScore::getHighScore()], 200);
}
}
Here is my routes managed in vue:
const routes = [
{
path: '/',
name: 'home',
component: Home,
meta: { bodyClass: 'page' },
props: true
},
]
Now within my home view I am populating the following within props:
props: ['highScore']
But it is undefined, any help would be appreciated
In the controller you pass the data as usual from controller to a blade view. I use this syntax:
Then, on the Blade View you have to add the VueJs component and pass the same data like this:
Now you can access all those variables on the Vue component.
In the jetsteam/inertia setup it's done like this:
Route::get('/page4', function () {
$allusers = ['aa','bb','cc'];
return Inertia::render('Page4',
['users' => $allusers]);
})
If it's a new projet? i recomment this, cause it makes it easier.
https://jetstream.laravel.com/2.x/stacks/inertia.html

ui.router netsted state which defined in another module not work when refresh page

My app has two angular modules (hub and bApp). hub module is a container for another modules (bApp). I use ui.router for routing and config as follow as:
In hub.js (hub module) file:
var hub = angular.module('hub', ['ui.router', 'bApp']).config(function ($stateProvider) {
$stateProvider.state({
name: 'hub',
url: '/',
templateUrl: 'hub/views/main.html',
controller: 'MainCtrl'
}).state({
name: 'hub.frontends',
url: 'frontend',
templateUrl: 'hub/views/frontend.html',
controller: 'FrontendCtrl',
resolve: {
frontendList: function (getList) {
return getList.getAll('/api/frontend');
}
}
})
$locationProvider.html5Mode({ enabled: true, requireBase: true }).hashPrefix('!');
});
In bApp.js (bApp module) file:
var bApp = angular.module('bApp',[]).config(function($stateProvider){
$stateProvider.state({
name: 'hub.book',
url: 'book',
templateUrl: 'book/views/book.html',
controller: 'bookCtrl',
resolve: {
bookList: function(getList){
return getList.getAll('/api/book');
}
}
}).state({
name:'hub.book.test',
ulr:'book/test',
template: '<h1>test</h1'
});
});
Finally in express server, I config as follow as (just for working with html5Mode):
app.all('/user/*', function (req, res, next) {
res.sendFile('static/user/index.html', { root: __dirname });
});
Everything works well if I only navigate between states by $state.go. But when I refresh or enter url to browser and press enter, only states which defined in hub module is work.
States which defined in bApp module is not work. But hub.run() still work normally, but just show a blank page with no error.
I don't know how to figure out this problem.
Thanks!

Area routing breaks in MVC6

In previous versions of ASP.NET, and using MVC 5, I could set up a route like this in my AreaRegistraion:
context.MapRoute(
"pfrecent",
"Forums/Recent/{page}",
new { controller = ForumController.Name, action = "Recent", page = 1 },
new[] { "PopForums.Controllers" });
This would route /Forums/Recent to the forum controller and its recent action. However, I can't figure out how to make it work in ASP.Net 5/MVC 6. I've added [Area("Forums")] to the controller class, and used this route from the Startup class:
routes.MapRoute(
"pfrecent",
"Forums/Recent/{page}",
new { controller = ForumController.Name, action = "Recent", page = 1 });
routes.MapRoute(
name: "areaRoute",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
However, it resolves to /Forums/Forum/Recent?page=1. The intention is to continue using /Forums/Recent.
We are using this for enabling areas in MVC 6:
// Configure MVC routing
app.UseMvc(routes =>
{
// Areas support
routes.MapRoute(
name: "areaRoute",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
// Default routing
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
The first route is for areas, second is for main content.
On Startup.cs
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "Business",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
})
on Controller
[Area("Business")]
public class DemoController : Controller
{
public IActionResult Index()
{
return View();
}
}
Place this in your Startup.cs file at the TOP of the routes list:
routes.MapRoute(
name: "forumsAreaRoute",
template: "Forums/{action}/{page?}",
defaults: new {area = "Forums", controller = "Forum", action = "Recent", page = 1});
Your Forum controller should look like this:
[Area("Forums")]
public class ForumController : Controller
{
// GET: /<controller>/
public IActionResult Recent(int? page)
{
// Do action stuff here
}
}
This solution will satisfy a url of http://somedomain.com/Forums/Recent/1 and return the first page.
I hate answering my own questions, but after getting back to this, and looking at source and experimenting, I found that you have to specify the area in the route mapping, as well as the HtmlHelpers (I'm assuming this will be true for tag helpers as well, but I haven't gone that far yet in conversion.) So the route mapping has to look like this:
routes.MapRoute(
"pfrecent",
"Forums/Recent/{page?}",
new { controller = ForumController.Name, action = "Recent", page = 1, Area = "Forums" }
);
The important part is the Area property in the route value object. The HtmlHelper has to look like this, also naming the Area:
#Html.ActionLink(PopForums.Resources.Recent, "Recent", ForumController.Name, new { page = 1, Area = "Forums" }, null)
It causes the URL for the recent page to be /Forums/Recent as expected. As best I can tell, setting routes in a derived AreaRegistration class in MVC5 set the area value on the routes for you. Having [Area("Forums")] on your controller class (which I already had) seems to assume the previous role of area registration.