I am aware that my code is slightly wrong (hence my post!). I am wanting my 'home' view to be displayed when a visitor accesses the '/' part of the website.
Currently, the view works when a user accesses the '/home' part of the website. I am currently pulling my hair out on how to do this!
Route.php:
Route::controller('/', 'HomeController');
Route::controller('users', 'UsersController');
Route::get('events/{id}/{slug}', 'EventsController#show');
Route::controller('events', 'EventsController');
HomeController.php:
<?php
class HomeController extends BaseController {
protected $layout = "layouts.main";
public function getHome(){
$events = myApp\Event::where('date','>=', DB::raw('CURDATE()'))->first();
$this->layout->content = View::make('home', array('events' => $events));
}
}
Home.blade.php:
<div class="col-md-4">
<h2>Next Event</h2>
<h3>{{$events->title}}</h3>
<p>Presented by {{ $events->consultant()->first()->title }} {{ $events->consultant()->first()->surname }}</p>
<b><p>{{ date("j F Y", strtotime($events->date)) }} from {{ date("g:ia", strtotime($events->start_time)) }}</p></b>
<a class="btn btn-success" href="{{ URL::to('events/' . $events->slug) }}">Book your place now.</a>
</div>
I have managed to get the view working with the '/' directory by using this within my routes.php:
Route::get('/', function(){
return View::make('home');
});
However, I am presented with the error:
Undefined variable: events (View:/Users/Sites/gp/app/views/home.blade.php).
It's as if, the HomeController isn't passing the 'events' array into the view, by just changing the route?! Any help/remedy/explanation would be hugely appreciated.
That's how Laravel RESTful controllers works, but you can create a new route for /, before your other routes, pointing to that action:
Route::controller('users', 'UsersController');
Route::get('events/{id}/{slug}', 'EventsController#show');
Route::controller('events', 'EventsController');
Route::get('/', 'HomeController#getHome');
Route::controller('/', 'HomeController');
EDIT
You have to understand that the Laravel Routing System tries to resolve a route as fast as it can, so if it finds a route that fits the current URI, it will use that route and forget about all the others. An example:
Route::get('/{variable}' 'Controller#action');
This is pretty generic route and can be resolved to anything, even
http://your-site.dev/events
So, if you add that route before this one:
Route::get('events/{id}/{slug}', 'EventsController#show');
Your events route will never be hit. That's why your most generic route have to be the last one.
Related
I have a method in my controller, PostsController:
public function index()
{
$post = Posts::where('id', '1')->get(['post']);
return $post;
}
web.php
Route::get('test', [PostsController::class, 'index']);
into test.blade.php in my views folder, and show it with
{{ $post }}
This works perfectly. However, when I try to do the exact same thing to the dashboard.blade.php, I get
Undefined variable $post
When trying to get this data on dashboard, I use this in app.php instead.
Route::get('dashboard', [PostsController::class, 'index']);
I was expecting to get the same data on the dashboard as in test.blade.php, but it cannot find the variable anymore as soon as I change the view in the route. What am I doing wrong?
try this
return view('test', [
'post' => Posts::where('id', '1')->get()
]);
and if your test.blade is not directly inside the views folder you need to specify the folder like that: view('folderName.test', ...)
and for more information, you can check the documentation https://laravel.com/docs/9.x/controllers
I'm getting this error even though the route is defined
Route [blood-camp] not defined. (View: C:\wamp64\www\blood-donation\resources\views\layouts\app.blade.php)
web.php file
`Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('home.root');
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/user', [App\Http\Controllers\UserController::class, 'index'])->name('user');
Route::resource('blood-camp', BloodCampController::class);
Route::resource('donor', DonorController::class);
Route::resource('camp-schedule', CampScheduleController::class);`
app.blade.php
<li class="nav-item dropdown">
<a class="nav-link" href="{{ route('blood-camp') }}" role="button" >
{{ __('Camps') }}
</a>
</li>
controller class
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class BloodCampController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('blood_camp.index', ['camps' => DB::table('blood_camps')->orderBy('name', 'ASC')->paginate(10)]);
}
//all the other resources are there...
}
route:list
There is no duplicate route
I have tried all the suggested ways including changing path names, cache clearing, private window, restarting server, but I'm still getting this error and no way to go further developments.
It first occurred when I adding new route called camp, I changed the name to blood-camp but no luck, now it throw exceptions for the other routes as well. Can someone please explain me what I'm doing wrong?
As the result of your route:list shows, you dont have a route with the alias blood-camp.
What you have as alias is blood-camp.index, blood-camp.store....
To generate the link of the ressources listing use:
By path/url
{{ url('blood-camp') }}
Or by alias
{{ route('blood-camp.index') }}
My controllers which are HomeController and BlogController in Admin folder. My views like:
/admin
index.blade.php
/blog
index.blade.php
I want to call /admin0admin url to /resources/views/admin/index.blade.php.
I want to call /admin0admin/blog url to /resources/views/admin/blog/index.blade.php
Here how i call in view:
<a href="{{ route('admin0admin.blog') }}" class="br-menu-link">
And my routes like:
Route::group(['namespace' => 'Admin', 'prefix' => 'admin0admin'], function () {
Route::get('/', 'HomeController#index')->name('index');
Route::group(['prefix' => 'blog'], function () {
Route::get('/', 'BlogController#index')->name('index');
});
});
And my BlogController index method:
return view('admin.blog.index');
I got an 404 not found error.
Route [admin0admin.blog] not defined
Laravel Version is : 5.6.*
You need to name the route admin0admin.blog, not index. prefix does not affect names of routes, so you need to write it out.
AdminLTE Laravel template screenshoot:
how can i direct the link into my page in folder lapor/one.blade.php and lapor/two.blade.php?
<li class="treeview">
<a><i class='fa fa-file'></i> <span>Laporan</span> <i class="fa fa-angle-left pull-right"></i></a>
<ul class="treeview-menu">
<li>One</li>
<li>Two</li>
</ul>
</li>
Make a route like below
Route::get('one', function () {
return view('lapor.one');
});
Route::get('two', function () {
return view('lapor.two');
});
And link it like below
<li>One</li>
I would group your adminLTE routes:
Route::group(['prefix' => 'admin', 'as' => 'admin.'], function()
{
Route::get('/', ['as' => 'dashboard', 'uses' => 'AdminController#index']);
Route::get('users', ['as' => 'user', 'uses' => 'AdminController#users']);
});
We prefixed those routes with /admin/ or whatever you want to call it. Then we prefixed their name with admin (using 'as').
Now get a specific route url:
{{ route('admin.dashboard') }}
Why do it like this?
Naming your routes is very important because if the route url changes and your app has hardcored urls (like url('/admin/dashboard') your entire application will break. With named routes this wont happen.
You can do it in three step:
make a function in your controller.like below
publice function functionName(){
return view('yourpagename(one)');
}
go to routes folder open web.php and connect with your controller function in routes. like
Route::get('page-name', 'controllerName#functionName');
add this url to your view page link tag
{{URL::to('page-name')}}
Hope it will works fine.
Before going to redirect the page two steps you need to do :
Step 1:
Define Methods in controller(named as SampleController) for example:
//Controller Name:SampleController
// Method Names defined in controller :lapor1,lapor2
//Method 1
public function lapor1(){
return view('lapor.one');
}
//Method 2
public function lapor2(){
return view('lapor.two');
}
Step :2
Define Routes for the pages like below:
Route::get('lapor1', ['as' => 'laporone','uses'=>'SampleController#lapor1']);
Route::get('lapor2', ['as' => 'laportwo','uses'=>'SampleController#lapor2']);
Step 3:
Link up to view pages now:
<li>One</li>
<li>Two</li>
Here is my routes.php:
Route::group(['prefix' => 'mine'], function () {
Route::get('/first', ['as' => 'mine.first', 'uses' => 'MyApp\Controllers\MyController#first']);
});
Here is my HTML/Twig file:
{{ form_open({'action': 'mine.first'}) }}
{{ form_submit('Start') }}
{{ form_close }}
And here is my controller:
class MyController extends BaseController {
public function first()
{
\View::make('stuff.mine.first'); //in folder app/views/stuff/mine
}
}
The error is "An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "MyController#first" as such route does not exist.") in "stuff.show" at line 130."
All the answers on this topic that I've seen are to name the route, but I've already done that.
Also, when I go to the URL manually (localhost/mine/first), the screen is blank even though there is HTML in that file.
Any idea what's going on? Thanks.
mine.first is a route name, not an action.
Use:
{{ form_open({'route': 'mine.first'}) }}
As for the view, controller action need to return a Response (the View generates one), so you just need add the proper keyword:
public function first()
{
return \View::make('stuff.mine.first');
}