Laravel 5 api route returning 404 apache2 - laravel-5

I can't get my api calls to return any data on ubuntu 16.04.
Here is my method in routes/api.php:
Route::get('comments', function() {
// If the Content-Type and Accept headers are set to 'application/json',
// this will return a JSON structure. This will be cleaned up later.
return Comment::all();
});
And this is in RouteServiceProvider:
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
When i hit localhost/api/comments I get 404.
I made sure AllowOverride All is set in my site.conf.
My database has a comments table with data that was populated using artisan seed

Turns out I had all of my api calls setup correctly. I could tell because of php artisan route:list being correct. I also had my apache2 sites setup correctly. The problem ended up being I hadn't setup the correct permissions for my base folder.
Use this command: sudo chmod 755 -R yourprojectbasefolderlocation

Related

laravel 7 php artisan route:cache is not working

Look at my code, Please.
web.php
Auth::routes();
//
Route::get('/', 'HomeController#index')->name('home');
Route::get('/findIDProvince', 'HomeController#findIDProvince')->name('findIDProvince');
Route::get('/markets', 'MarketController#index')->name('market');
Route::get('/market/{marketSlug}', 'MarketController#single');
Route::get('/category/{categorySlug}', 'CategoryController#single');
Route::match(['get', 'post'],'/cart/{market}',"MarketController#AddCard")->name('cart');
Route::get('/shopping-cart', 'MarketController#showCart')->name('cart');
Route::post('/comments', 'MarketController#comments')->name('comments');
Route::get('/{pageSlug}', 'PageController#contact')->name('contact');
Route::middleware('auth:web')->group(function () {
Route::post('/payment', 'PaymentController#payment')->name('payment');
Route::get('/payment/callback', 'PaymentController#callback')->name('payment.callback');
});
Route::prefix('ajax')->group(function() {
Route::post('/add-to-cart', 'AjaxController#add_to_cart');
Route::post('/remove-from-cart', 'AjaxController#remove_from_cart');
Route::post('/get-cart', 'AjaxController#get_cart');
Route::post('/increment-cart-item', 'AjaxController#increment_cart_item');
Route::post('/decrease-cart-item', 'AjaxController#decrease_cart_item');
Route::delete('/delete/{id}', 'AjaxController#delete');
});
Route::namespace('Admin')->middleware(['auth:web', 'checkAdmin'])->prefix('admin')->group(function (){
Route::get('dashboard', 'DashboardController#index')->name('dashboard');
Route::resource('slideShows', 'SlideShowController');
Route::resource('categories', 'CategoryController');
Route::resource('users', 'UserController');
Route::resource('markets', 'MarketController');
Route::resource('orders', 'OrderController');
Route::resource('pages', 'PageController');
Route::get('footers', 'FooterController#index')->name('footers.index');
Route::get('links', 'LinkController#index')->name('links.index');
Route::post('links/store', 'LinkController#store')->name('links.store');
Route::resource('address', 'AddressController');
Route::get('socials', 'SocialController#index')->name('socials.index');
Route::post('socials/store', 'SocialController#store')->name('socials.store');
Route::get('approved', 'CommentController#approved')->name('approved');
Route::get('unapproved', 'CommentController#unapproved')->name('unapproved');
Route::put('comment/update/{comment}', 'CommentController#update')->name('comment.update');
Route::delete('comment/destroy/{comment}', 'CommentController#destroy')->name('comment.destroy');
});
I have installed laravel 7 on my local server. When I run php artisan route:cache command then laravel returns the error:
I'm writing my project on Laravel. When I optimize the project, I have a problem :
Unable to prepare route [api/user] for serialization. Uses Closure.
I looked for any closures in web.php, but I didn't find anything.
Laravel can't cache routes, which use closures - https://github.com/laravel/framework/issues/22034
There is example user route in routes/api.php Just remove it and try again

index "/" route not working when using with a prefix in Laravel

I have the following route declaration:
Route::name('test.')
->prefix('test')
->group(function() {
Route::get('/', function() {
dd('test');
})->name('index');
});
Trying to access /test will result in NotFoundException, although when calling route('test.index') it will resolve to /test.
Same exception when trying to access /test/ and when trying to change the line:
Route::get('/', function()...
to
Route::get('', function()...
As soon as I modify it to
Route::get('/test', function()...
it works when trying to access ´/test/test´ and it also resolved the uri correctly when calling route('test.index').
What am I missing to to get the route working using '/' ?

How to use Laravel authentication and vue-router together

I'm new to Laravel and PHP in general, but familiar with Vue and SPA's. I know how to create authentication with Bcrypt, for example. However, I used Laravel to run php artisan make:auth and to create several different endpoints for the backend.
I was trying to convert my app into a SPA using Vue, however using routing with Vue causes issues with the routes defined in the web.php. For example, I have my web.php like this, with several different routes.
<?php
Route::get('/{vue_capture?}', function () {
return view('app');
})->where('vue_capture', '^(?!storage).*$');
Route::get('/', function () {
return view('app');
});
Route::resource('Videos', 'VideoController')->middleware('auth','isAdmin');
Route::resource('Categories', 'CategoriesController')->middleware('auth');
Route::get('/search', 'VideoController#search')->middleware('auth');
Auth::routes();
Route::get('/settings/account', 'AccountsController#edit')->middleware('auth');
Route::get('/auth', 'AccountsController#get');
Route::put('/settings/account', 'AccountsController#update')->middleware('auth');
However, I also have a routes.js to have vue-router handle the routing with the following :
import Home from './components/Home.vue'
import Videos from './components/Videos.vue'
import Categories from './components/Categories.vue'
export default{
mode: 'history',
routes: [
{
path:'/',
component:Home
},
{
path:'/Videos',
component:Videos
},
{
path:'/Categories',
component:Categories
},
{
path:'/login',
component:login
},
{
path:'/register',
component:register
},
{
path:'/logout',
component:logout
}
]
}
I understand that Vue is supposed to take over the routing if you use a SPA, but is there any way to use both? I can't find anything that addresses this issue but I can't believe that Laravel would make you choose to either use their built-in commands like php artisan make:auth or have to scrap all of that and do it all manually if you want a SPA with routing.
I've tried going through different CRUD turorials on using Laravel and or Vue.
I've tried directing all of the routes to Vue to have vue-router handle the routing.
I've tried not giving all routing to vue-router and retaining the back-end routes that I had in web.php
Other semi-related problem is my routes aren't even appearning as links using router-link . It just appears as normal text. But for now my priority is the routing issue.
You can combine both vue route and Laravel route. But for the best result, I advise you use vue router since you are building an spa.
remove
Route::get('/', function () {
return view('app');
});
put all the backend route before the route that point to your vue.
Route::resource('Videos', 'VideoController')->middleware('auth','isAdmin');
Route::resource('Categories', 'CategoriesController')->middleware('auth');
Route::get('/search', 'VideoController#search')->middleware('auth');
Auth::routes();
Route::get('/settings/account', 'AccountsController#edit')->middleware('auth');
Route::get('/auth', 'AccountsController#get');
Route::put('/settings/account', 'AccountsController#update')->middleware('auth');
Route::get('/{vue_capture?}', function () {
return view('app');
})->where('vue_capture', '^(?!storage).*$');
Also, verify that you don't have conflicting routes on your backend (run php artisan route:list to see your laravel route list) and vue routes. I hope this helps.
The accepted answer is perfect (I upvoted but I have no rep yet for it to count) and worked well for me but only under the condition if I also created 'dummy' vue components and imported them to work with the routes. Hope this helps!
import Login from './auth/Login'
import Register from './auth/Register'
import Logout from './auth/Logout'
{ path:'/login', component:Login },
{ path:'/register', component:Register },
{ path:'/logout', component:Logout },
You can just use this instead
Route::get('/{vue_capture?}', function () {
return view('app');
})->where('vue_capture', '^(?!storage).*$')->middleware('auth');

Why don't catch the requested file URI my defined Route in Laravel 5.7?

I have this codes in my routes/api.php file:
Route::group(['middleware' => 'auth:api'], function () {
Route::prefix('photoalbum')->group(function() {
Route::prefix('image')->group(function() {
Route::post('download/{albumId}/{size}/{filename}',
'PhotoalbumImageController#download');
// ...
});
});
});
Route::fallback('HomeContorller#index');
Now I try to open this URL:
http://myproject.test/api/photoalbum/image/download/1/xs/dog.jpg
...and I get the result from the HomeController#index function. The other routes working fine.
UPDATE
The php artisan route:list get the correct list of routes, contain this:
| | POST | api/photoalbum/image/download/{albumId}/{size}/{filename} | | App\Http\Controllers\PhotoalbumImageController#download | api,auth:api,auth |
Additionally: the requested file isn't exists. The controller should be process and serve it.
Why don't catch the request my defined Route and send it to the PhotoalbumImageController#download function and how can I fix it?
Your defined route type is POST and you are trying to access that via GET.
changing your route to Route::get solves your problem.
Please try this, and use name for routes it is useful, and remember if the call is GET, POST, PUT, etc.
Route::group(['middleware' => 'auth:api','prefix'=>'photoalbun/image'], function () {
Route::match(['post','get'],'/download/{albumId}/{size}/{filename}','PhotoalbumImageController#download')->name('api.photoalbun.image.download');
});
To see all route you can use
php artisan route:list

Adding new route to Laravel

I have the following in /var/www/html/blog/routes/web.php
Route::get('/', function () {
return view('welcome');
});
Route::get('test', function () {
return view('test', ['name' => 'Chris']);
});
If I go to http://52.214.14.137 then I see /var/www/html/blog/resources/views/welcome.blade.php.
If I go to If I go to http://52.214.14.137/test then I would expect to see /var/www/html/blog/resources/views/test.blade.php but instead I am seeing a 404.
What am I missing?
I was using Amazon EC2 and the .htaccess file was being ignored by default.
I had to change /etc/httpd/conf/httpd.conf so
AllowOverride None
Become
AllowOverride All
I then restarted Apache with the command below and then Laravel worked perfectly.
sudo service httpd restart

Resources