How to route Lumen for single page app - php

I'm using Lumen + Vue js to build an app.
I have this code in routes.php
$app->get('{any}', function () {
return view('vue', []);
});
$app->get('/', function () {
return view('vue', []);
});
This works great for /login, /users, /anything . But when I add a subroute like /users/agents or /a/b, /a/b/c -> anything with more than one slash it gives me the 404 from lumen

You have the 404 error because {any} will not catch the parameters that contain slash. I order to make it do so, you need to add a pattern:
$app->get('{any:.+}', function () {
return view('vue', []);
});

Related

How to redirect to specific URL?

I have a route for tenants name like that:
example.test/TenantNameHere
And I want to make the default of my domain start with the word "site":
example.test/site
My code:
Route::prefix('{tenantName}')->group(function () {
Route::get('/', function ($tenantName) {
return "{$tenantName}";
});
});
Route::prefix('site')->group(function () {
Route::get('price', function () {
return view('welcome');
});
});
Route::redirect('/', 'site', 301);
The problem that I'm facing with this code now is when I open the domain it redirects me to tenantName route, not to the home page that I made!
How can spreate the route of site from TenantName ?
you just have to register your absolute path ('site') first, and "wildcards" routes after, because everything you put in url line now hit the first tenantName route
try this (reverse the order):
Route::redirect('/', '/site', 301);
Route::prefix('site')->group(function () {
Route::get('/', function () {
return view('welcome');
});
});
Route::prefix('{tenantName}')->group(function () {
Route::get('/', function ($tenantName) {
return "{$tenantName}";
});
});
also change "/site/price" path to "/site", so redirect will find correct route

How to redirect to different domain in Slim framework of PHP?

Using Slim we can use routes like:
$app->get('/path', function() {
include 'content.php';
});
We can also redirect to any other path of same domain like:
$app->get('/path2', function () use ($app) {
$app->redirect('/redirect-here');
});
But I want to redirect to some different domain and none of below is working:
$app->get('/feeds', function(){
$app->redirect('http://feeds.example.com/feed');
});
This shows blank page:
$app->get('/feeds', function() {
header("Location: http://feeds.example.com/feed");
});
In Slim 3, you should use the withRedirect method on the Response object:
$app->get('/feeds', function ($request, $response, $args) {
return $response->withRedirect('http://feeds.example.com/feed', 301);
});
For Slim 2 only, you can do:
$app->get('/feeds', function() use ($app) {
$app->redirect('http://feeds.example.com/feed');
});

Defined route throwing controller method not found laravel 4

I have a route defined in routes.php file but when i make an ajax request from my angular app, i get this error
{"error":{"type":"Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException","message":"Controller method not found.","file":"C:\\xampp\\htdocs\\tedxph\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Controllers\\Controller.php","line":290}}
this is my routes file
/*
|--------------------------------------------------------------------------
| Api Routes
|--------------------------------------------------------------------------
*/
Route::group(array('prefix' => 'api'), function() {
//Auth Routes
Route::post('auth/login', 'ApiUserController#authUser');
Route::post('auth/signup', 'ApiUserController#registerUser');
/* Persons */
Route::group(array('prefix' => 'people'), function() {
Route::get('{id}', 'ApiPeopleController#read');
Route::get('/', 'ApiPeopleController#read');
});
/* Events */
Route::group(array('prefix' => 'events'), function() {
Route::get('{id}', 'ApiEventsController#read');
Route::get('/','ApiEventsController#read');
});
});
Accessing the same url (http://localhost/site/public/api/auth/signup) from a rest client app on chrome does not give any errors, what could be wrong?
this is the angular code from my controller
$rootScope.show('Please wait..registering');
API.register({email: email, password: password})
.success(function (data) {
if(data.status == "success") {
console.log(data);
$rootScope.hide();
}
})
.error(function (error) {
console.log(error)
$rootScope.hide();
})
more angular code
angular.module('tedxph.API', [])
.factory('API', function ($rootScope, $http, $ionicLoading, $window) {
//base url
var base = "http://localhost/tedxph/public/api";
return {
auth: function (form) {
return $http.post(base+"/auth/login", form);
},
register: function (form) {
return $http.post(base+"/auth/signup", form);
},
fetchPeople: function () {
return $http.get(base+"/people");
},
fetchEvents: function() {
return $http.get(base+"/events");
},
}
});
It'd help to see the code you're using to make the angular request, as well as the header information from Chrome's Network -> XHR logger, but my first guess would be Angular is sending the AJAX request with the GET method instead of the POST method. Try changing Angular to send an explicit POST or change routes.php so auth/signup responds to both GET and POST requests.
Update looking at your screen shots, the AJAX request is returning an error 500. There should be information logged to either your laravel.log file or your PHP/webserver error log as to why the error is happening. My guess if your Angular request sends different information that your Chrome/REST-app does, and that triggers a code path where there's an error.
Fixed the problem, turns my controller was calling an undefined method in the controller class.
Renamed the method correctly and the request now works, thanks guys for the input.

Laravel grouping routes with one name

i have this below route and that can work correctly
Route::get('admin/login', array('as'=>'login', function()
{
return View::make('back_end.login');
}));
app
views
back_end
layouts
index.blade.php
main.blade.php
profile.blade.php
login.blade.php
for admin i have any view for show and i want to grouping that with admin perfix. after this action and use
http://localhost/laravel/public/admin/login
http://localhost/laravel/public/admin/profile
URL i get this error:
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
this is my routes:
Route::group(array('prefix' => 'admin'), function()
{
Route::get('login', function()
{
return View::make('back_end.login');
});
Route::get('index', array('as'=>'dashboard'), function()
{
return View::make('back_end.layouts.index');
});
Route::get('profile', function()
{
return View::make('back_end.layouts.profile');
});
});
how to fix this routes. please help me
I had the same issue recently. Here is a slimmed down version of the routing that I used, including a catch all. I was routing to controllers, however you can replace that syntax with a function, the rout will be handled the same.
Route::group(array('prefix' => 'admin'), function(){
Route::get('/','AdminController#index');
Route::resource('users', 'UserController');
Route::get('settings','AdminController#settings');
/* Catch all route */
Route::any('{all}', function($uri){
return Redirect::to('admin')
->with('flash_error', "The administration page 'admin/$uri' could not be found.");
})->where('all', '.*');
});
As always, make sure to run composer dump-autoload after updating the routes. This worked successfully for me. You will only need the '/' on the relative 'base' route.
Make changes (add a preceding slash / to each routes inside the admin group) as given below:
Route::group(array('prefix' => 'admin'), function()
{
Route::get('/login', function()
{
return View::make('back_end.login');
});
Route::get('/index', array('as'=>'dashboard'), function()
{
return View::make('back_end.layouts.index');
});
Route::get('/profile', function()
{
return View::make('back_end.layouts.profile');
});
});
It should be /login instead of login and same for each one.

How to use Laravel's wildcards on route group before auth?

I have this code below in my router.php
Route::group(array('before' => 'auth'), function()
{
Route::get('account/(:all?)', function() {});
Route::get('facebook/(:all?)', function() {});
});
Route::controller(Controller::detect());
It works well when the user is not logged in. But once he is successfully logged in and gets redirected to the requested page, the page is not displaying anything; just a blank page. I have tried to use :any instead of :all and it does the same thing.
Can anybody identify the problems?
Your routes are mapped to empty closures. You need to return something or map them to controllers.
Route::get('account/(:any?)', function() {
return "Hello World";
});
Route::get('account/(:any?)', function() {
return View::make('accounts.index');
});
//assuming you have an AccountController.php
Route::get('account/(:any?)', 'account#index');
//automatically route all methods of a controller
Route::controller('account');
Check out the laravel docs on routing.
Apparently, I did not find the better solution for using the group filter. The way I do it now to redirect guests to auth is this:
Route::filter('before', function()
{
$open_routes = array(
'',
'home',
'auth',
'help'
);
if(!in_array(URI::segment(1), $open_routes) && Auth::guest()) {
return Redirect::to('/auth/login');
}
});

Categories