How can i direct link into page in laravel 5.4 - php

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>

Related

Laravel 5.6 Route Group

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.

Missing required parameters exception, while param is filled in in URL

I'm working on a Laravel project, where I have the following routes:
Route::group(['middleware' => ['web', 'auth'], 'prefix' => 'deliver'], function () {
Route::get("{pitch}/play", "DeliverController#play")->name("deliver.play");
Route::get('/', 'DeliverController#index')->name('deliver');
});
The route named deliver.play is being called like this:
<a href="{{ route("deliver.play", $pitch) }}">
<span class="glyphicon glyphicon-picture" data-toggle="tooltip" data-placement="top" title="Go to Playmode"></span>
</a>
As you can see, I pass the $pitch parameter to the route, however, on execution, the following error pops up:
ErrorException in UrlGenerationException.php line 17:
Missing required parameters for [Route: deliver.play] [URI: deliver/{pitch}/play].
Which normally means the {pitch} param isn't given, however, in the URL bar of my browser the param is there, giving me the complete URL of
http://localhost:8000/deliver/empty-pitch-13/play
So how is it possible for Laravel to not see the parameter im passing on to the route? Or is there something that I'm missing?
Thanks.
Edit:
I forgot to add the controller that the route links to. Here it is:
public function play(Pitch $pitch)
{
if ($pitch->hasPresentation()) {
$presentation = head($pitch->presentations);
return view("deliver.play.index", $presentation);
}
return redirect()->route('slides.create', $pitch);
}
Try this:
<a href="{{ route('deliver.play', ['pitch' => $pitch]) }}">
and add the pitch params to the function like so
Route::group(['middleware' => ['web', 'auth'], 'prefix' => 'deliver'], function () {
Route::get("{pitch}/play", "DeliverController#play", function($pitch){
//
})->name("deliver.play");
Route::get('/', 'DeliverController#index')->name('deliver');
});

Laravel version - 4.2 logout is not working

NotFoundHttpException showing when load the page when i am logout it is showing 404 error it is showing object not found showing the rout section is given below:
route.php
Route::get('logout',array('uses' => 'LoginController#logout'));
the controller is LoginController
public function logout()
{
//Session::flush();
Auth::logout();
return Redirect::to('login');
}
and anchor tag is
<li><i class="fa fa-sign-out"></i> Logout</li>
but it is showing
Object not found! 404 error
Route::group(['namespace' => 'YourNameSpace', 'before' => 'auth'], function() {
Route::get('logout', [
'uses' => 'LoginController#logout',
]);
});
'auth' must be defined in filters

Laravel 5.2 authentication

I've been trying to get the new release (5.2) of Laravel to work with a simple web app. However, I'm having a problem with authentication.
All pages of the app include a navigation view partial which uses Auth::user()->name to display the username in the nav if they are logged in.
In order to do this, I created a pages controller which loads the auth middleware in the constructor:
public function __construct()
{
$this->middleware('auth');
}
This works perfectly if the user is logged in. However, if the user is not logged in, they are requested to login on every page. Even pages like "contact" or "about" which clearly should not require authentication to view.
How can I make pages like "about" always accessible while still being able to access Auth in the nav?
EDIT:
Routes
Route::group(['middleware' => ['web']], function () {
Route::get('/home', 'StaticController#home');
Route::get('/about', 'StaticController#about');
Route::get('/contact', 'StaticController#contact');
});
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/', 'HomeController#index');
});
StaticController
class StaticController extends Controller
{
public function home()
{
return view('static.home');
}
public function about()
{
return view('static.about');
}
public function contact()
{
return view('static.contact');
}
}
Navigation
<ul class="nav navbar-nav">
#if (Auth::guest())
<li>Login</li>
<li>Register</li>
#else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li><i class="fa fa-btn fa-sign-out"></i>Logout</li>
</ul>
</li>
#endif
</ul>
After deleting everything and installing a fresh copy of Laravel the problem disappeared.
You have two possible solutions:
Route middleware (can get a bit hard to maintain) https://laravel.com/docs/master/middleware#assigning-middleware-to-routes
Within your view partial you could simply have a conditional statement to check or create a method on the Auth facade. This would mean you wouldn't need the conditional. Conditional solution below:
Auth::check() ? Auth::user()->name : ''
In /app/Http/Kernel.php
check to see if you have
'auth' => \App\Http\Middleware\Authenticate::class,
Example
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
....
];
Now, you may need to re-architect your route to something like this
in your routes.php
**//Routes (Not Require Log-in)**
Route::get('/home', 'StaticController#home');
Route::get('/about', 'StaticController#about');
Route::get('/contact', 'StaticController#contact');
**//Authentication Routes**
Route::group(['middleware' => ['auth']], function () {
Route::get('/', 'HomeController#index');
//................................
// More Auth Routes Go in HERE
//................................
});
Hope it helps !
you can do something like this
$this->middleware('auth', ['only' => 'update'])
the only will be set on the specified method for example

Laravel Controller method not found for homepage

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.

Categories