I am trying to get a laravel-nuxt project running. I am stuck with creating route calls to my laravel backend using axios async call to serve up data to my nuxt frontend before loading the page.
I am constantly getting getting a 404 with my current laravel-nuxt setup even though I have the route defined in api.php.
I am using this as a template for the project and I have not changed anything in that template yet:
https://github.com/cretueusebiu/laravel-nuxt
So my frontend call is this here:
async asyncData ({ $axios }) {
if (process.server) {
return $axios.$get('/api/data')
.then((res) => {
this.data = res.data;
})
}
}
And my backend route is defined as follows in api.php:
Route::get('/data', 'HomeController#index');
It always gives me a 404, is there something missing that I should be aware of?
According to the Readme in the Github project you have mentioned, you have to add your routes manually to
client/router.js
Read this line under Notes and follow the structure well you'll be able to avoid this.
This project uses router-module, so you have to add the routes
manually in client/router.js.
hope this helps.
Related
Im trying to build SPA and I need to make axios call to endpoint in my routes. How can make this api call work?
Here are my routes
Route::get('/{any}', function () {
return view('default');
})->where('any', '.*');
Route::get('events', 'EventsController#index')->prefix('api');
Any suggestion?
You must go to the folder routes->api and create your api routes, there is automatically the api prefix.
in your .vue
axios.get('/api/getUser')
.then(data => {
console.log(data)
});
in your folder routes->api
Route::get('/getUser', 'Api\UsersController#getUser');
To not put "/api" in front of all your axios calls in your '.vue' you can put
axios.defaults.baseURL = '/api';
in your app.js
We define a catch-all route to the SpaController which means that any web route will map to our SPA. If we didn’t do this, and the user made a request to /hello, Laravel would respond with a 404.
Route::get('/{any}', 'SpaController#index')->where('any', '.*');
Remember that this route will always be putted on the last part of your web routes after declaring your other routes
use this
Route::any('{all}', function () {
return view('default');
})
->where('all', '^(?!api).*$')
->where('all', '^(?!storage).*$');
Route::get('events', 'EventsController#index')->prefix('api');
as you will need api and storage route handel by laravel
After upgrading to CakepPHP 4.0 my POST request trough XMLHttpRequest stopped passing data to $this->request->getData()
The data is accesible by $this->request->input('json_decode'); though and GET requests work as well.
But I wonder what has changed in comparision to 3.* and why it's not working like it was before.
This is my xhr:
this.$http.post(
url,
data,
{headers: {'X-CSRF-TOKEN': '<?= $this->request->getAttribute('csrfToken') ?>'}},
})
.then(response => {
//
}
);
It gives me an empty array when I call $this->request->getData()
I tried to turn off FormProtection component for that specific action but nothing changed.
If want to know what changed, check the out the migration guide first, in this case specifically the breaking changes section for components.
The request body parsing features have been removed from the request handler component (the deprecation warning that was in place previously has been removed too, as it was causing too many false positives). This should now be handled by the body parser middleware, which you need to add your application accordingly, either globally in your Application class':
public function middleware(MiddlewareQueue $middlwareQueue): MiddlewareQueue
{
// ...
$middlwareQueue->add(new \Cake\Http\Middleware\BodyParserMiddleware());
return $middlwareQueue;
}
or in a routing scope:
\Cake\Routing\Router::scope('/', function (\Cake\Routing\RouteBuilder $routes) {
$routes->registerMiddleware('bodyParser', new \Cake\Http\Middleware\BodyParserMiddleware());
$routes->applyMiddleware('bodyParser');
// ...
});
See also
Cookbook > Middleware > Using Middleware
Cookbook > Routing > Connecting Scoped Middleware
I am trying to find a concrete example on how to use the Laravel with Angular 2.
Tried using google, but no luck. Additionally, I was wondering when using the Component annotation object key templateUrl, can it accept php extension instead of html.
The consensus appears to be that it's best to use Angular 2 as an independent front-end, and have Laravel act as an API.
That said, I did find this guide that definitely links the two. I'm not sure if it will answer your specific question, but it might help you get started. Good Luck!
After setup all router in front-side with angular.
This route is for Angular2 and it should be place after all other backend routes
just keep it at the bottom ( route.php in laravel5.2 web.php in laravel5.3)
Route::get('/{any}', function ($any) {
return view('welcome');
})->where('any', '.*');
$scope.refresh = function () {
$http({
url: '',
method: "GET"
}).success(function (data) {
$scope.trans = data.transactions;
console.log($scope.trans);
}).error(function (data) {
console.log('Error');
});
}
$scope.refresh(); // We call the function on initialization to load the feed.
In using AngularJS on top of Laravel, assuming I have this route in Laravel:
Route::get('/admin/', function() { return View::make('pages.writer'); });
which makes a view that loads AngularJS components, including $routeProvider, and that the configuration is like so:
$routeProvider.when('/writer/:publicationName?', {
templateUrl: '/templates/writer.html',
controller: 'WriterCtrl',
caseInsensitiveMatch: true
}).otherwise({
templateUrl: '/templates/library.html',
controller: 'LibraryCtrl'
});
If I call the URL: /admin/writer/My-Favorite-Publication, I get a Laravel error because the route doesn't exist. Is there a way to tell Laravel when Angular routing takes over for a particular URL (i.e. anything after /admin/), so that I can use $routeParams.
You need to define a catch-all route that will return the base view that runs AngularJS app and let AngularJS handle the rest of the routing.
This will do the trick:
Route::get('/admin/{ignore?}', function() {
return View::make('pages.writer');
})->where('ignore', '.*');
This will make everything that starts with /admin match this Laravel route.
I'm trying to add a new controller to an existing laravel project. The application already has some pages at /users and I am trying to add a RESTful API which works separately to this. I would like the API to be available at api/users.
I have created the controller using PHP artisan:
php artisan controller:make ApiUsersController
I have added the following to my routes:
Route::controller('api/users', 'ApiUsersController');
However when I hit the URL I just receive the site's 'Page could not be found' message.
Is there something I have missed?
It looks like the issue you're having is that you've used Route::controller rather than Route::resource.
Route::resource maps routes to the seven RESTful methods that the controller generator creates by default. Route::controller maps them to methods that you add yourself that have the HTTP method as part of their name, in your case if you had a method called getIndex it would be called on a GET request to /api/users/index or if you had one called postStore it would be called on a POST request to /api/users/store.
In order to add the API prefix to the route you could use the following:
Route::group(['prefix' => 'api'], function() {
Route::resource('users', 'ControllerName');
});
You could also add any other controllers in the API within the same callback.