You need to add a route for it in web.php
Laravel: Calling a Controller
Hello
I am a newbie.
I have created a Controller(laravel/app/Http/Controllers/ProductController). How do I call it from the browser directly? What would be the url?
Please help.
Thanks.
My web.php is not working. https://laracasts.com/discuss/channels/laravel/routes-in-laravel-1?page=1&replyId=858399
Can't I call it directly from the browser?
I can do it in CodeIgniter. By using the url:
var base_url = ""; $(document).ready(function(){ $("body").on("click","#submit_login", function(){ $('.err_msg').css('display', 'none'); var phone_no = $.trim($("#phone_no1").val()); $.ajax({ "url" : base_url+"index.php/welcome/login_user", "type" : "POST", "data" : {"phone_no":phone_no}, "success" : function(data){ if(data == "true"){ alert("Registration Successfull"); window.location.href = base_url+"index.php/welcome/lobby"; } else{ alert("Incorrect Mobile Number, Please try again!"); return false; } } }); }); });@deicool Start here: https://laravel.com/docs
If you had read the docs, or done any lessons here on Laracasts, then you wouldn’t be asking such basic questions.
The doc says I need to define a route.
I was wondering if there is any way to go around this necessity.
@deicool No. You don’t directly “call” controllers. You define routes in your application, that route HTTP requests to corresponding controller actions.
I created the following configuration for Controller:
use App\Http\Controllers\ProductController;
Route::get('/products', [ProductController::class, 'show']);
However the url does not work.
Please advise.
@deicool No one can “advise” on “does not work”.
Why doesn’t it work? What do you get when you try and go to /products in your browser?
Unfortunately I’m neither able to see what’s on screen, nor am I clairvoyant.
Sorry. My bad.
The url returns the following content:
This page isn’t working
{ipaddress} is currently unable to handle this request.
HTTP ERROR 500
@deicool 500 means that you have an error in your code, and if you read the error message it will tell you what is wrong.
My code is something like this:
namespace App\Http\Controllers;
class ProductController extends Controller
{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function show()
{
}
}
@deicool He didn’t ask for code though, did he? He told you to read the error message.
Stop typing so much and start reading more.
There is not much in the error message for me to decipher the problem.
Is it possible to see more about the error (any log file)?
@deicool either /storage/logs/laravel.log or the logs for the webserver you are using
@deicool This is the pertinent part:
{ipaddress} is currently unable to handle this request.
That isn’t a standard Laravel error message. That’s something to do with your networking setup.
Getting the following error:
[2022-12-22 10:13:26] local.ERROR: syntax error, unexpected token "return" {"exception":"[object] (ParseError(code: 0): syntax error, unexpected token \"return\" at /var/www/html/laravel/routes/web.php:23)
[stacktrace]
#0 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(503): Illuminate\Routing\RouteFileRegistrar->register()
#1 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(457): Illuminate\Routing\Router->loadRoutes()
#2 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Routing/RouteRegistrar.php(192): Illuminate\Routing\Router->group()
#3 /var/www/html/laravel/app/Providers/RouteServiceProvider.php(37): Illuminate\Routing\RouteRegistrar->group()
#4 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): App\Providers\RouteServiceProvider->App\Providers\{closure}()
#5 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#6 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(81): Illuminate\Container\Util::unwrapIfClosure()
#7 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod()
#8 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php(651): Illuminate\Container\BoundMethod::call()
#9 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php(120): Illuminate\Container\Container->call()
#10 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php(45): Illuminate\Foundation\Support\Providers\RouteServiceProvider->loadRoutes()
@deicool So it’s a syntax error in your routes file. Which any decent IDE or text editor would have shown you.
You’ve randomly stuck return somewhere in the file that it doesn’t belong. So show your routes/web.php file now.
Yup there was a error in my web.php file. But after fixing it, i get the same error (HTTP Error 500)
The web.php file:
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/products', function () {
return view('welcome');
});
Route::get('/dashboard', function () {
return 'welcome to dashboard!';
});
@deicool And the new error in the log is?
#0 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(503): Illuminate\Routing\RouteFileRegistrar->register()
#1 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(457): Illuminate\Routing\Router->loadRoutes()
#2 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Routing/RouteRegistrar.php(192): Illuminate\Routing\Router->group()
#3 /var/www/html/laravel/app/Providers/RouteServiceProvider.php(37): Illuminate\Routing\RouteRegistrar->group()
#4 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): App\Providers\RouteServiceProvider->App\Providers\{closure}()
#5 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#6 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(81): Illuminate\Container\Util::unwrapIfClosure()
#7 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod()
#8 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php(651): Illuminate\Container\BoundMethod::call()
#9 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php(120): Illuminate\Container\Container->call()
#10 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php(45): Illuminate\Foundation\Support\Providers\RouteServiceProvider->loadRoutes()
#11 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\Foundation\Support\Providers\RouteServiceProvider->Illuminate\Foundation\Support\Providers\{closure}()
#12 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#13 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(81): Illuminate\Container\Util::unwrapIfClosure()
#14 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod()
#15 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php(651): Illuminate\Container\BoundMethod::call()
#16 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Support/ServiceProvider.php(119): Illuminate\Container\Container->call()
#17 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(936): Illuminate\Support\ServiceProvider->callBootedCallbacks()
#18 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(914): Illuminate\Foundation\Application->bootProvider()
#19 [internal function]: Illuminate\Foundation\Application->Illuminate\Foundation\{closure}()
#20 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(915): array_walk()
#21 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/BootProviders.php(17): Illuminate\Foundation\Application->boot()
#22 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(242): Illuminate\Foundation\Bootstrap\BootProviders->bootstrap()
#23 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\Foundation\Application->bootstrapWith()
#24 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(160): Illuminate\Foundation\Http\Kernel->bootstrap()
#25 /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(134): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter()
#26 /var/www/html/laravel/public/index.php(52): Illuminate\Foundation\Http\Kernel->handle()
#27 {main}
My index.php just has 4 lines:
<?php
use App\Http\Controllers\ProductController;
$a = new ProductController();
$a->show();
Mebbe a earlier version of "index.php" is cached. How do I refresh it?
@deicool Seems you showed everything except the error? Maybe empty the laravel.log file, and trigger the error again, and see if you can see the error in there
@deicool That index.php is invalid. It never autoloads from composer and laravle is never loaded.. Please revert it to how it was
@deicool Mate. That’s not a valid index.php file. What on earth are you doing?
Stop deliberately ignoring advice and read the docs on routing. There’s no point picking up a framework like Laravel if you’re not actually going to use it how it’s intended to be used.
Agreed. My bad. Thank You.
My bad.
@deicool No worries :) Php works a bit different that other languages. In php you in theory need to "require" all the files you need.
require './app/Http/Controllers/PostController.php';
But luckily composer does this for us. So instead we just import its autoload file which loads everything https://github.com/laravel/laravel/blob/9.x/public/index.php#L34
Without this line, nothing will work. But when using laravel, you need all of the contents of this file, to actually boot up the framework :)
Thanks for everything :-)
@deicool Also, why have you created a second thread when you already had one open on this topic?
https://laracasts.com/discuss/channels/laravel/routes-in-laravel-1
/cc @jeffreyway
They are different questions to me.
Please or to participate in this conversation.