I'm trying to build RESTful API with Laravel using PhpStorm and artisan server, but when I try to test with Rest Client I receive this error:
For now I write only the GET method and I receive correct output in my browser with the address http://localhost:8000/users
This is my code:
routes.php
Route::resource('users','UserController');
UserController.php
public function index()
{
return \Response::json(User::all());
}
I tried also to add json in request window
php artisan serve --host 127.0.0.1 should do the trick.
Looks like PHPStorm does a lookup on localhost which results in 127.0.0.1. But php artisan serve binds to your local IPv6 address ::1.
Related
i have start the server using xampp
http://localhost/api/login
used this url in postman for request and still get this error
**Not Found
The requested URL was not found on this server.
Apache/2.4.54 (Win64) OpenSSL/1.1.1p PHP/8.2.0 Server at localhost Port 80**
my route in api.php is
Route::post('login',[userController::class,'userLogin']);
I have tried with php artisan serve with url
http://127.0.0.1:8000/api/login
but didn't work
You need to clear your route cache by
php artisan route:cache
This is common when testing with Postman. Once your routes cache is cleared, your route will be picked up by Postman.
So i am new to Angular4, I have a service that is supposed to make a request to a php api. I have the following code -
getWeather(keyword: string){
return this.http.get("weather.php", {
params: new HttpParams().set('keyword', keyword),
headers: new HttpHeaders().set('Method', 'search'),
});
I am getting a 404 localhost:4200/weather.php cannot be found. not sure wher to place the php endpoint file.
Assuming that localhost:4200 is your front-end server, you have to set your php server api path in the call. If your php server in in local on the 8080 port, you type this.http.get('http://localhost:8080/weather.php',...
You can put it in your src/assets folder and then call it using this.http.get("assets/weather.php", {....
This is because for Angular application is hosted on 4200 port and your PHP file will be using port 80.
Try using
http://localhot/path/to/weather.php
I made a basic project in Laravel 5.4 using PhpStorm and executed php artisan make:auth and php artisan migrate. After running php artisan serve, I accessed the resulting site on http://localhost:8000 and tested the password recovery. Using Gmail for SMTP, the corresponding user received an e-mail with a link like this:
http://localhost/password/reset/d55b8591a690c96742e192d31ba6b1c7b06cb4b390fa08baaf02ce261618b884
It redirects me to a 404 Error page.
Looking at php artisan route:list, there is a route called "password.reset", set on GET|HEAD with the following URI: password/reset/{token}. So it seams that should work, since there is a place for a token. The action is: App\Http\Controllers\Auth\ResetPasswordController#showResetForm
There's basically nothing on that controller, but it has use ResetsPassword from Illuminate\Foundation\Auth inside the class. When I try to overwrite the function, I look at the original and it does redirect to reset password view.
But something is not working. I tried to add :8000 to the link, but it just redirects me to a blank page and I'm sure the server is still serving at that port. What am I doing wrong?
You have to change APP_URL=http://localhost to APP_URL=http://localhost:8000 in the .env
Your route in the email must have the ":8000" that is the port of your application. Your browser if looking for the same route but in just in the localhost route without a port
I use Lumen 5.4.
This is how my route is setup:
$app->get('/ip/{ip}', GeoIpController::class . '#show');
The {ip} route parameter should be an IP address, with dots in it. However, it seems there is a problem when a route has dots in it. It returns a 404 not found error.
I am aware I could pass the IP address in as a simple GET request parameter, but want the IP to be part of the URL and to be handled like a route parameter.
For testing purposes, I use php -S localhost:8080 -t public to serve the application.
This is a limitation on PHP's built in server, not with Lumen (or Laravel, or Slim, or any other frameworks/apps with a router). You can view the PHP bug report here.
Basically, if the URL has a dot in the url after the script name, the built-in server treats the request as a static file request, and it never actually attempts to run through the application.
This request should work fine on a real web server (apache, nginx), but it will fail when run on PHP's built-in development web server.
So im trying to build 2 separate applications 1 that used as a backend (Laravel as a REST api) and Angular application as the client, eventually those 2 apps have to work together under the same domain as a single web app.
What im trying to accomplish:
My Angular app is a single page application that boot from index.html, all the routes are handled by Angular except /api/* that should be handled by Laravel app.
Im using 2 different apps in order to build web app more dynamic so i can easily change my backend framework and technologies and testing each app as a 'stand-alone' more easily.
I dont want to use CORS in my response headers because my REST API serves ONLY my Angular app and not other applications such as api for developers.
I want to use proxy that will foward all requests come from http://localhost:9100/api/* to: http://localhost:9000/api/*
Firstly im running Laravel on port 9000 by running:
php artisan serve --port 9000
And Angular app under port 9100 by running a gulp task (index.html is in the path ./src):
gulp.task('webserver', function(){
connect.server({
root: './src',
port: 9100,
middleware: function(connect, o) {
var url = require('url');
var proxy = require('proxy-middleware');
var options = url.parse('http://localhost:9000/api');
options.route = '/api';
return [proxy(options)];
}
});
});
Both apps work perfectly as a stand-alone, but when im trying to navigate to:
http://localhost:9100/api/v1/comments i receive the following error:
Error: connect ECONNREFUSED at errnoException (net.js:904:11) at Object.afterConnect [as oncomplete] (net.js:895:19)
I tried to investigate the cause of this problem, some people say it connected to my hosts file so i had to add the line:
127.0.0.1 localhost
But it doesnt work.
I tried different gulp task:
gulp.task('webserver', function() {
gulp.src("./src")
.pipe(webserver({
port: 9100,
livereload: true,
open: 'http://localhost:9100',
proxies: [
{
source: '/api', target: 'http://localhost:9000/api'
}
]
}));
});
And i receive the exact same error...
My develop environment is Windows 10 x64 bit.
Did you try to use http-proxy-middleware instead of proxy-middleware?
I experienced the same error with proxy-middleware. (Gulp browser-sync - redirect API request via proxy)
Error: connect ECONNREFUSED
at errnoException (net.js:904:11)
at Object.afterConnect [as oncomplete] (net.js:895:19)
Ended up creating http-proxy-middleware, which solved the issue in my case.
proxy-middleware somehow didn't work on the corporate network. http-proxy just did. (http-proxy-middleware uses http-proxy to do actual proxying)
Guess you are using gulp-webserver; The proxy can be added like:
var proxyMiddleware = require('http-proxy-middleware');
gulp.task('webserver', function() {
gulp.src("./src")
.pipe(webserver({
port: 9100,
livereload: true,
open: 'http://localhost:9100',
middleware: [proxyMiddleware('/api', {target: 'http://localhost:9000'})]
}));
});
Never found out why this error is thrown with proxy-middleware in the corporate network ...
Update:
Think this question has been answered:
It was a problem with the artisan server, had to run it this way:
php artisan serve --host 0.0.0.0
Source:
https://github.com/chimurai/http-proxy-middleware/issues/38
https://github.com/chimurai/http-proxy-middleware/issues/21#issuecomment-138132809