Laravel 5 controller Routing does not work - php

I am familiar with Laravel 4 routes, but I am experiencing some problem with Laravel 5.
I code route.php as:
Route::get('/','HomeController#index');
and my HomeController.php is the following:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class HomeController extends Controller {
public function index()
{
return View::make('index');
}
}
The output page displays as:
Whoops,looks like something went wrong.
The Route annotation file seems to be perfect.
The same case occurs for folder routing too!!
Please help me out.

First of all you should follow the instructions from James Njuguna in a comment to your question. Withoug debugging we can only guess whats going wrong.
In your case, most likely your error is, that the line
return View::make('index');
is causing an exception, because class App\Http\Controllers\View is not found. In this file a namespace is used, so you have to reference the root namespace like:
return \View::make('index');
OR you use a helper function
return view('index');
This function is documentated at http://laravel.com/docs/5.0/helpers#miscellaneous
If that's still failing... maybe you don't have an index.php or index.blade.php in your resources/views folder.

All, what #shock_gone_wild and #JamesNjuguna said is true. The reason of an error occurring is that you do not use namespaces when you call View.
For testing you can simply return text from a controller like this:
public function index()
{
return 'test'
}
and when it returns a result you can see what was the reason for the error and than you can change it with view global function, like #JamesNjuguna said.

Try this
public function index()
{
return view('home');
}
In laravel 5 the view class is not illuminated using a capital letter at the beginning

Related

Although I created the route correctly But when I try to show the page It gives me an error page not found

This is my function in MemberController
public function text(){
return "hello";
}
This is my route
Route::get('/text',[MemberController::class,'text']);
So,basically its unable to read new route that i created.
Thanks in advance
Hope you're using laravel 8 and above
follwoing should be your controller code (MemberController.php)
<?php
namespace App\Http\Controllers;
class MemberController extends Controller{
public function text(){
return "hello";
}
}
and this should be your route code (web.php)
<?php
use App\Http\Controllers\MemberController;
Route::get('/text',[MemberController::class,'text']);
Code you have provided is not enough, though you can check following things.
See if your routes aren't cached. Run php artisan route:clear in order to clear the cached routes.
Where have you defined the route? If it's in the web.php file then, your actual url should be: http://localhost:8000/text.
If it's in the api.php file then, your url should be: http://localhost:8000/api/text.

InvalidArgumentException - No hint path defined

I am trying to add controller view in my existing project that consider model view controller structure in php with laravel.
class CashFlowdataController extends Controller {
public function index() {
return view('CashFlowdata::create');
}
}
When I implement this, it shows me error for,
InvalidArgumentException
No hint path defined for [CashFlowdata].
I have added file in route.php and web.php as added other controller data. Only for this one it shows message like this.
you code is wrong you should to something like this
class CashFlowdataController extends Controller {
public function index() {
return view('CashFlowdata.create');
}
}
here CashFlowdata.create
its means in laravel
folder structure should be
view>CashFlowdata>create.blade.php
laravel view() function is a helper to load view file
ref link https://laravel.com/docs/8.x/helpers#method-view
I had the same issue in nwidart/laravel-modules due to module.json file was miss place.
I move the file to the root of module now working fine.

Laravel and PSR 4 Autoloading not working

I've just set up a new Laravel 5 project. I included a Helpers.php in the /app directory. Here it is:
<?php namespace EP\Helpers;
class Helpers {
public static function sayHi()
{
return 'Hi';
}
}
And in the route I am doing:
Route::get('/', function(){
return EP\Helpers\Helpers::sayHi();
});
But when I hit that route I am getting the error:
Class 'EP\Helpers\Helpers' not found
The funny thing is, PHPStorm is able to auto detect the namespace. Anyone know why this would be happening?
I believe in your case the namespace would be:
<?php namespace EP;

two consecutive uppercase REST controller laravel

Hi guys I hope someone can help me, I'm using laravel 4 and I have a REST controller like this
Route::controller('example', 'SomeClass');
class SomeClass extends BaseController {
public function getThisIsATest() {
return 'Hello World';
}
}
but when I try to enter the URI localhost/example/this-is-a-test
I get a 'Controller method not found.'
I'm thinking it's because the two consecutive uppercase letters in the 'getThisIsATest'
Does someone know how to fix this issue??
Two ways (without changing the URL)...
1. Use an additional route
Add an implicit route for the URL before the controller route:
Route::get('example/this-is-a-test', 'SomeClass#getThisIsATest');
Route::controller('example', 'SomeClass');
2. Change your method name to snake_case
You can change the method name in your controller to snake case and everything will work as excepted. (It doesn't look very pretty though)
public function get_this_is_a_test() {
return 'Hello World';
}
Actually it even works to only write the "problematic" part in snake case: getThisIsA_test. However this looks even more weird ;)
Try this:
Route::resource('example/this-is-a-test', 'SomeClassController#getThisIsATest');
OR:
Route::get('example/this-is-a-test', 'SomeClassController#getThisIsATest');
See, if that solves your problem & Do visit Laravel Controller Docs for more information
Add {{ URL::to('example/this-is-a-test'); }} and you're good to go
In your app/routes.php try defining your route as follows:
Route::controller("example", "testController");
And rename your controller class to
class testController extends BaseController {
public function index(){
// Just have it here to be safe.
}
public function getTest() {
return 'Hello World';
}
}
Now try navigating to localhost/example/test. Let me know if that works, I've never used Route::controller() before, so it's new to me as well.
Edit
As the above doesn't work for this example, try navigating to localhost/example/this-is-atest without changing your controller/route.

Laravel setting up a simple controller

<?php
class TasksController extends BaseController {
public function index()
{
return 'show all tasks.';
}
}
?>
That's what I did very simple. But when I go to localhost:8888/tasks
it doesn't show the result it shows Whoops! Something went wrong.
What could possibly be the problem, such a simple code? By the way I just set up Laravel recently.
Controllers don't do anything until you point a route at them, via app/routes.php.
http://laravel.com/docs/routing
Adding:
Route::get('tasks', 'TasksController#index');
is likely to do the trick.

Categories