Working with Laravel routes - laravel

I built my application using Angularjs on the frontend and Laravel 5 at the backend, however my main issue now is routing, when the page is loaded initially I set it to return my angular.php view I even added some code to catch all routes and return that view for me.
This does not work in all cases:
routes.php
Route::any('{url?}', function($url) {
return view('angular');
})->where(['url' => '[-a-z0-9/]+']);
Example of a URL that works with this is:
http://localhost:8000/tickets/events/catgeories/
Example of a URL that does not work with this is:
http://localhost:8000/tickets/events/Musical/Some-event-name
By "not working" I mean Laravel throws a NotFoundHttpException. What I am thinking right now is the above route can't go past three levels/parameters as in /level-1/level-2/level-3.
What am I doing wrong here?

Maybe because second URL has uppercase characters?

Related

How to stop the GET method is not supported for this route from showing?

I have a working Laravel project with loads of different routes.
I'm currently testing it and one of my tests was to check if a user were to use a delete or post route from the URL. I didn't know what the application would do honestly and it outputted the typical:
The GET method is not supported for this route. Supported methods: DELETE
which I have seen a million times. Is there a way to stop this error from coming up and instead output an error screen or simply redirect to a different view?
The error message:
The GET method is not supported for this route. Supported methods: DELETE.
should only appear when your Laravel site has APP_DEBUG set to true (in the .env file).
When APP_DEBUG is set to false as it should always be in on a live site, then the user will be shown a 404 error page instead.
You can easily test this by adding the following code to your routes file:
Route::delete('test', function() {
return 'You should never see this text when viewing url via a GET request';
});
May be u didn't noticed but ur form tag method attribute and route definition is different

Laravel not picking up the correct route

I am developing an application using Laravel 8.x. I have an api.php file that I am using. It was all working fine for the last 5 months. Today I added a new route to the api.php file and that route is not getting picked up by the Laravel. I have the following items in the api.php file
//Products
Route::get('/products/{currency_id?}/{type?}/{eventtype?}', [Controllers\ProductsController::class, 'getproduct2']);
Route::get('/products/search/{query}/{itemtype?}/{itemcategory?}', [Controllers\ProductsController::class, 'findproduct']); //getproduct
Route::post('/products/findproductbyid/{product_id}', [Controllers\ProductsController::class, 'getproduct']);
Route::post('/products/create', [Controllers\ProductsController::class, 'setproduct']);
Route::post('/products/update', [Controllers\ProductsController::class, 'putproduct']);
And it shows up the URL list also
but when I access it (findproductbyid) in the browser it is not getting picked up. it shows the results of /products/{currency_id}/{type}/{eventtype}
Please tell me to know why is it so? I tried clearing Laravel cache, etc.
Thanks in advance
UPDATE: I tried changing the GET to POST, but it seems it is not getting updated to POST
You don't define url parameters with $, here you done {$product_id}
Change it to the following.
Route::get('/products/findbyproductid/{product_id}', [...]);
Rearrange the routes like this and try it out
Route::post('/products/findproductbyid/{product_id}', [Controllers\ProductsController::class, 'getproduct']);
Route::get('/products/search/{query}/{itemtype?}/{itemcategory?}', [Controllers\ProductsController::class, 'findproduct']); //getproduct
Route::get('/products/{currency_id?}/{type?}/{eventtype?}', [Controllers\ProductsController::class, 'getproduct2']);

Exclude conditional routes in Laravel

I'm building and application in Laravel and Vuejs where I'm having Laravel routes as below:
Route::get('/admin/{view?}', 'HomeController#admin')->where('view', '(.*)')->name('admin');
Route::get('/{view?}', 'HomeController#home')->where('view', '(.*)')->where('view', '!=', 'admin')->name('home');
I'm using Vue-router so I'm having routing in vuejs, and I'm using history mode. The problem is when I try to call /admin it generally calls HomeController#home method. even if I go deeper like /admin/dashboard it is calling the same home method. I want if admin prefix is being called then it should call HomeController#admin method.
its all okay for me please check this
Route::get('/admin/{view?}', function (){
dd('okay');
})->where('view', '(.*)')->name('admin');
Route::get('/{view?}', function(){
dd('okay1');
})->where('view', '(.*)')->name('home');
So try this
Route::get('/admin/{view?}', 'HomeController#admin')->where('view', '(.*)')->name('admin');
Route::get('/{view?}', 'HomeController#home')->where('view', '(.*)')->name('home');

Laravel 5 -- Redirect in Controller is not working in AJAX call

Important: this comes from ajax call.
Everything works perfectly except:
use Illuminate\Routing\Redirector;
public function my_call() {
return redirect()->route('page-1');
}
Throws 500 error.
return view('page-1') works without problems.
Maybe anybody sees what I am doing wrong?
Thank you.
Do you have an actual route with the name that you are using, you cannot redirect to a view as these are returned by routes
Apperantly you cannot do this on server side if you do AJAX call:/
Here an explanation: https://laracasts.com/discuss/channels/vue/redirect-after-ajax-post-request
It's for vue but I believe it applies universally for ajax.

New routes in Laravel are not working

I just tried to add a new route in Laravel but it seems it's not working, just getting 404 errors.
It only detects the index '/' route.
This code is in the routes/web.php
Route::get('/', 'SiteController#mainView')->name('home');
Route::get('secondroute','SiteController#secondRoute')->name('secondRoute');
The Controller is also working, because it doesnt matter if the index Route (/) is the mainView or SecondRoute so it has to be something with the routing itself?
Thanks
Edit: Mod Rewrite is on
Edit2: I'm using a Ubuntu on a Virtual Machine with Apache 2.4.25
Edit3:
public function secondRoute(){
return view('myself', ['title' => 'Myself']);
}
You missed '/' in second route
change
Route::get('secondroute','SiteController#secondRoute')->name('secondRoute');
to
Route::get('/secondroute','SiteController#secondRoute')->name('secondRoute');

Resources