Reputation: 119
I am new in Laravel. I read documentation and googling but I can't find answer for my problem.
Im trying to render a simple view test.blade.php (located at resources/views).
a) first try: in app/Http/routes.php I wrote
Route::get('/', function(){
return view('test');
});
It's working. Hurray! I see my simple site
b) second try: in app/Http/routes.php i wrote
Route::get('/', 'WelcomeController@index');
I have created a controller WelcomeController.php in app/Http/Controllers Inside controller I wrote:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class WelcomeController extends Controller
{
public function index()
{
return view('test');
}
}
And....this is not working, I see only blank page. Ok I was digging and digging and trying to understand what's the problem. I changed retun view('test') to return view()->make('test'); but this is not working too - it generates only blank page....
Then I simply changed retun to echo: echo view('test'); It is working like charm.
What am I doing wrong? all examples in the internet shows return view('..') not echo view('...') (which is ugly)...
Please help me :)
Upvotes: 2
Views: 855
Reputation: 119
After few hours of digging I got it!
I decided to create second project and using composer to install laravel in another directory. Next I opened great tool names Meld which I used to compare these two installations. I noticed that there are some diferences in main controller Controller.php located at app/Http/Controllers
In my first installation (that with problems) it was:
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
abstract class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public $auto_render = true;
/**
* Execute an action on the controller.
*
* @param string $method
* @param array $parameters
* @return \Symfony\Component\HttpFoundation\Response
*/
public function callAction($method, $parameters)
{
parent::callAction($method, $parameters);
}
}
And in the second installation:
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
abstract class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
I decided to remove callAction from Controller.php because it only call parent method. Then I checked my page in browser...and
Hurrrayy it's working....return view('test') working as it should...
I think, that in some previous version of Laravel this method was added, and in the next someone removed it...but why it blocks rendering template?
It happens because overwritten function did not return result... When I changed that function like this:
public function callAction($method, $parameters)
{
return parent::callAction($method, $parameters);
}
That was the solution! Everything is now working great.
I want to thank you all, a specially maytham-mahiam and revo for your big help.
Upvotes: 1
Reputation: 1041
Another approach is routes definition using array
Try this:
You could also specify the controller and action within array.
Here we are saying the route to use the index function within the WelcomeController
Add in routes.php
Route::get('/', [ 'uses' => 'WelcomeController@index' ]);
Hope this is helpful.
Upvotes: 0