I recently started using the Laravel framework and I would like the following (but cannot seem to get it right):
pagination, not the kind which Laravel explains but more the kind of /about.html - /portfolio.html etc.
It seems really difficult to achieve this, I searched for a bit and could not find anything or perhaps im not using the right search terms.
The HomeController serves the layout view that has all the html.
The default route is:
Route::get('/', 'HomeController#show');
And this is the HomeControiler:
class HomeController extends BaseController {
public function show() {
return View::make('layout');
}
}
This isn't pagination, it's just more than one route. Your routes for that would be something like:
Route::get('/', 'HomeController#showIndex');
Route::get('/about', 'HomeController#showAbout');
Route::get('/portfolio', 'HomeController#showPortfolio');
The corresponding controller might be like:
class HomeController extends BaseController {
public function showIndex() {
return View::make('index');
}
public function showAbout() {
return View::make('about');
}
public function showPortfolio() {
return View::make('portfolio');
}
}
You definitely don't put the HTML for different routes all in the same view file (shared navigation should be handled via shared layouts and the #extends blade keyword), and it's best not to use the .html extension when routes are perfectly happy without it.
Related
I have been declaring all the routes for my application inside web.php , but it is now getting quite large. I find that I am losing a lot of time shifting between web.php and each controller and this is hurting productivity.
I feel like it would be better to define routes inside of the controller, perhaps ideally delegating some URL to a controller and then allowing the controller to handle the "sub routes" since this would allow me to use inheritance when I have two similar controllers with similar routes.
It is not possible given how laravel works. Every request is passed onto router to find its designated spot viz. the controller with the method. If it fails to find the route within the router, it just throws the exception. So the request never reaches any controller if the route is not found. It was possible in earlier versions on Symphony where you would configure the route in the comment of a particular controller method.
Sadly with laravel it works how it works.
But for me, I just like to have the routes in a separate file.
Alternate solution, easier way to sort all the routes.
You can move your route registration into controllers if you use static methods for this. The code below is checked in Laravel 7
In web.php
use App\Http\Controllers\MyController;
.....
MyController::registerRoutes('myprefix');
In MyController.php
(I use here additional static methods from the ancestor controller also posted below)
use Illuminate\Support\Facades\Route;
.....
class MyController extends Controller {
......
static public function registerRoutes($prefix)
{
Route::group(['prefix' => $prefix], function () {
Route::any("/foo/{$id}", self::selfRouteName("fooAction"));
Route::resource($prefix, self::selfQualifiedPath());
}
public function fooAction($id)
{
........
}
In Controller.php
class Controller extends BaseController {
....
protected static function selfAction($actionName, $parameters = [], $absolute = false)
{
return action([static::class, $actionName], $parameters, $absolute);
}
protected static function selfQualifiedPath()
{
return "\\".static::class;
}
protected static function selfRouteName($actionName)
{
//classic string syntax return "\\".static::class."#".$actionName;
// using tuple syntax for clarity
return [static::class, $actionName];
}
}
selfAction mentioned here is not related to your question, but mentioned just because it allows making correct urls for actions either by controller itself or any class using it. This approach helps making action-related activity closer to the controller and avoiding manual url-making. I even prefer making specific functions per action, so for example for fooAction
static public function fooActionUrl($id)
{
return self::selfAction('foo', ['id' => $id]);
}
Passing prefix into registerRoutes makes controller even portable in a sense, so allows inserting it into another site with a different prefix in case of conflict
Editing this post entirely as I realize that I did a poor job of explaining things as they are, and to reflect a few changes already implemented.
The issue: I have an app that has a normal front end, which works perfectly when accessed via app\public. I've added a backend and wish to use a different master layout. I have named the backend Crud. I created Crud\UserController and that has the following:
public function __construct()
{ $this->middleware('auth'); }
public function getIndex() {
return view('crud'); }
In my routes.php file I have the following:
Route::controller('crud', 'Crud\UserController');
I've tried placing that route inside and outside of the middleware group. Neither workds. I do have a file, crud.blade.php, that exists inside resources\views.
The issue is a 404 from apache every time I try to access app/public/crud. Specifically, this error:
The requested URL /app/public/crud was not found on this server.
I'm at a loss as to why the server is unable to find the route to crud.blade.php
ETA: The apache access log just shows a normal 404 when I attempt to access this page. The apache error log shows no errors.
The view() is suppose to point to a view file, that is something like example.blade.php which should contain the content you want displayed when the localhost:app/crud is visited. Then you can do view('example') without the .blade.php.
From your code, change
class UserController extends Controller{
//Route::controller('crud', 'Crud\UserController');
public function __construct()
{
$this->middleware('auth');
}
public function getIndex() {
return view('/');
}
}
to
class UserController extends Controller{
//Route::controller('crud', 'Crud\UserController');
public function __construct()
{
$this->middleware('auth');
}
public function getIndex() {
return view('crud'); // crud being your view file
}
}
I think you're mixing concepts. Trying to get here will always throw an error:
localhost://app/crud
To enter your app, just try to access:
http://localhost
appending a route you registered in your routes.php file. In your example, It'd be:
http://localhost/crud
If you register
Route::get('/crud', 'Crud\UserController#index')
Then you need an Index method inside UserController, which should return a view (in your example)
class UserController extends Controller{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('crud');
}
}
P.S: about Implicit controllers, you have the docs here.
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
I am new to laravel. I am just trying to do some basic stuff using routes and controllers.
I have this controller named testcontroller which looks as below
<?php
class testcontroller extends BaseController{
public function show(){
return 'test';
}
}
I have also defined a route in routes.php as below
Route::get('test', 'testcontroller#show');
when i try to access it using localhost/laravel/public/test it would give the 404 NOT FOUND error, however i have tried rewriting the route in main directory as
Route::get('/', 'testcontroller#show');
and it works all fine, i have even tried and successfully rendered views using this. Can anyone suggest where am i going wrong with the url. Any help will be appreciated.
Thanks :)
Test controller
# app/controllers/TestController.php
class TestController extends BaseController {
public function show()
{
return 'test';
}
}
Routes File
# app/routes.php
Route::get('test', array(
'uses' => 'TestController#show'
));
Route::get('/', array(
'uses' => 'TestController#show'
));
Edit: since that is not working, it is probably to do with your server configuration. Instead of learning how to do that, I'd recommend the use of a VM over WAMP. Also Laravel provides an easy way to use one with Laravel Homestead.
Check out this instructional video on how to do that.
I'm a new user of both Stack Overflow and laravel.
I just started to learn L4 in their official site but now I'm having a small problem in loading the views.
Now my views is not working properly. Any file I call under the views dir results in "file doesn't exist" error.
This is my code ....
Authors controller is:
class AuthorController extends BaseController{
public $restful = true;
public function get_index()
{
return View::make('authors.index');
}
}
On routes.php I added
Route::get('authors',array('uses'=>'authors#index'));
under views/authors/index.php
Any html text
Change
Route::get('authors',array('uses'=>'authors#index'));
to
Route::get('authors',array('uses'=>'AuthorController#get_index'));
If you dont plan on doing complex logic you could skip the controller and just call the view from the router. And also its a good practice to keep the functions as camelCase so in your case getIndex().
Route::get('authors', 'AuthorController#getIndex');