I use davejamesmiller Breadcrumbs package. I am wondering how to pass a parameter to a breadcrumb, something like an id.
In the docs (here) it says that is possible, but can't find the way to do it.
My goal es to do a breadcrumb like this: Dashboard \ User \ New Model. Where New Model its a form to add model data with some relationship with the user. Without the user_id param the link for User won't work.
Any idea?
You can pass global variable
\View::share ( 'variable2', $variable2 );
if render breadcrumbs in layout
or You need render breadcrumbs in `user.new_model.blade
#section('content')
{!! Breadcrumbs::render('page', $page) !!}
#stop`
my way
Create template
breadcrumbs.blade.php
with content
#if(!empty($breadcrumbs))
<ol class="breadcrumb">
<li>{!! link_to_route('main', 'Home') !!}</li>
#foreach($breadcrumbs as $bread)
#if(isset($bread['url']))
<li>{!! link_to($bread['url'], $bread['name']) !!}</li>
#else
<li>{!! $bread['name'] !!}</li>
#endif
#endforeach
</ol>
#endif
and connect it to layout
#include('breadcrumbs')
and in your action pass array of links
\View::share('breadcrumbs', [
['url' => route('collection.show', ['id'=>$data->collection, 'url'=>$data->collection]), 'name' => $data->collection->name],
['name' => $data->article]
]);
There is another way. As general, in each view just calling Breadcrumbs::render() should create the hierarchy of the breadcrumbs links depending on the routes defined in routes/breadcrumbs.php.
There are two essential points that you have keep in mind to go further with this solution:
The callback function that found in breadcrumbs.php routs definition is the place from which you should pass your parameters.
Giving correct name to your web route from routes/web.php which will be used later in routes/breadcrumbs.php
Checkout the following code snippets that demonstrates the above two points:
//Point1: routes/breadcrumbs.php
Breadcrumbs::register('job.edit', function($breadcrumbs, $job, $title)
{
$breadcrumbs->parent('job','job');
$breadcrumbs->push($title, route('job.edit', $job));
});
Breadcrumbs::register('job.edit.install', function($breadcrumbs, $job, $title)
{
$breadcrumbs->parent('job.edit',$job, $title);
$breadcrumbs->push('Job Install Equipments', route('job.edit.install','job'));
});
In the above code we passed $job and $title through the callback function.
//Point2 routes/web.php
Route::get('/job/edit/{job}', 'JobController#edit')->name('job.edit');
Route::get('/job/install-equipments/{job}', 'JobController#installEquipments')->name('job.edit.install');
We give a name to the route through the name method , Laravel 5.4, which allow us to define the routes correctly in Point1.
The last step, is what you have do in the view file. Here I will show you the last one regarding /job/install-equipments which should be rendered in the breadcrumb as the last element and its parent is job/edit with parameter job which handles the primary key id
//install.equipments.blade.php
#extends('layouts.main')
#section('content')
{!! Breadcrumbs::render('job.edit.install',$job->id, __('Edit').': '.$job->title) !!}
The above will render a breacrumbs look like:
Home / Job / Edit: title of
job / Job Install Equipment
The required parameters that handles breadcrumbs render are supplied un the above render method i.e through $job->id and __('Edit').': '.$job->title) , the last just adjusting the text and it could be done inside the callback function of breadcrumbs routes.
Related
I thought to write static routes like contact, imprint or "About Us" in the web.php as a one-liner. I saw this at Laravel Daily.
web.php
Route::get('/{page}', App\Http\Controllers\StaticPageController::class)
->name('page')
->where('page', 'about-us|imprint|contact');
It's nice but I'm getting problems with my navbar.
My Blade navbar has a dynamic part. The current menu item is highlighted. Very simple.
nav.blade.php
<x-nav-link :href="route('about-us')" :active="request()->routeIs('about-us')">
{{ __('About us') }}
</x-nav-link>
With the new one-liner, I then get the following error message:
Symfony\Component\Routing\Exception\RouteNotFoundException.
Route [about-us] not defined. (View: /project01/resources/views/includes/nav.blade.php)
Which is logical, because I no longer have the about-us route.
Actually, the route should be given a corresponding array mapping. But I don't know how. How can I solve the problem?
The solution for this problem will be:
<x-nav-link :href="route('page', ['page' => 'about-us'])" :active="request()->routeIs('about-us')">
{{ __('About us') }}
</x-nav-link>
Special Thanks for all good answears!
You have two options.
1- Change the routeIs() to is() and use the path, not the route name
<x-nav-link :href="route('page', ['page' => 'about-us'])" :active="request()->is('about-us/*')">
{{ __('About us') }}
</x-nav-link>
2- Switch back to 3 routes instead of the one liner. There is no gain in using the one liner. It is slower and harder to read (maintain)
You have assigned a name page to a given route with the required parameter named page. So, you must use the following code:
route('page', ['page' => 'about-us']);
https://laravel.com/docs/6.x/routing#named-routes
I think there is a misunderstanding.'about-us|imprint|contact' is the parameter restrictions.'page' is the route name.So this should work;
<x-nav-link :href="route('page','about-us')" :active="Request::url() == route('page','about-us')">
{{ __('About us') }}
</x-nav-link>
I am trying to include another view if it exists. I have a main page.blade.php which should render the content of the selected page. in the back-end of the application I get a flat array with all the content in it (id and name). So now if a template exists I want to render it but first I need to check if the template actually exists.
I got the following code right now:
#foreach($flattenPage as $page)
#if(file_exists('templates.'.strtolower($page['name'])))
#include('templates.'.strtolower($page["name"]))
#endif
#endforeach
The problem is that it won't get through the if statement.
I also tried:
#if(file_exists('../templates/'.strtolower($page['name']).'.blade.php'))
The current template is views/page/show.blade.php and I need to render views/templates/myContent.blade.php
If you need to determine if a view exists - you can use exists method (as described in the docs)
#if (view()->exists('templates.'.strtolower($page["name"])))
#include('templates.'.strtolower($page["name"]))
#endif
From Laravel 5.2 you can also do this:
#includeIf('view.name', ['some' => 'data'])
Your code will look like this:
#foreach($flattenPage as $page)
#includeIf('templates.'.strtolower($page["name"]))
#endforeach
I'm trying to create a page dynamically for each courses that I'm adding in a database.
I have a CoursesController who is taking care of adding, displaying the courses.
So, when I click on a course, it should dynamically create a page for that course and show details in that course page.
In the route.php page, I have
Route::get('courses/{code}', [ 'as'=>'course-show', 'uses'=>'CoursesController#getShow']);
and in the
CoursesController.php
public function getShow($code){
return $code;
}
And in the index.blade.php for CoursesController,
<h4>{{ $course->name }}</h4>
Now, It create the link with a unique code (saved in database) and upon clicking there, it takes me to the course page with an error:
BadMethodCallException
Method [show] does not exist.
What might be the problem? Can anyone help me?
The getShow() function in your controller should be show().
Also URL::action() goes to a controller action.
You probably want URL::route()
<h4> {{ $course->name }} </h4>
or you could do this
<h4> {{ $course->name }} </h4>
I am using Laravel for a CMS Application, I have stored the navigation menu in Cache, Since It's generated dynamically from the database and every time the navigation menu is updated in Database, The cache will be updated as well.
The only issue i have is, Though i can access the navigation menu easily, I want to set active class on the link of the page being accessed and this is really driving me mad.
This is how i have stored the nav in cache
Home
About Us
Services
And this is how i access the navigation within the page.blade.php file
{{ Navigation::display_main_menu() }}, This function checks whether a cache exists or not, If not then it creates the cache and then returns the value.
But the navigation gets displayed exactly like this with php tags and not with the class (which i want to see), I even tried changing how i store nav to Home but even this gets displayed exactly as it is.
What can i do to execute the php code or the blade syntax when the navigation is being rendered.
Please point me in the right direction. Any help is greatly appreciated.
You may add a new macro in HTML class, simply add this in a file (macros.php) and store it inside app/start directory then include it from the app/startglobal.php file using require/include:
HTML::macro('menuItem', function($name, $title = null, $parameters = array(), $attributes = array()){
$currentUrl = URL::current() == url() ? url('home') : URL::current();
$active = ( $currentUrl == URL::route($name, $parameters) ) ? ' class="active"':'';
return '<li'.$active.'>' . HTML::linkRoute($name, $title, $parameters, $attributes) . '</li>';
});
In your view you may generate the menu items using something like this:
<ul>
{{ HTML::menuItem('page.home', 'Home', array('home')) }}
</ul>
Here page.home is the route name assigned to / when declaring the route and yes in this case routes should have name, for example:
Route::get('/', array('uses' => 'PageController#home', 'as' => 'page.home'));
In the menuItem macro the second parameter (Home) is the menu title to show and the third parameter is an optional parameter if has any. So for example, your About Us page has the parameter about-us and to create the menu item you may use this:
{{ HTML::menuItem('page.about', 'About Us', array('about-us')) }}
This is a possible solution and there may be other ways.
I'm having some strange behavior with my forms in Laravel 4. I have a "settings" page with two forms, each (are supposed to) POST to a controller method, update the database and return back to the settings page. However, there seems to be an issue, either with the way my forms are working or my routes.
Here's how it is, simplified:
Settings page: (site.com/settings)
<div id="form-one" class="form-area">
{{ Form::open(array('action' => 'SettingController#editOption')) }}
{{ Form::text('optionvalue', 'Default')) }}
{{ Form::submit('Save Changes') }}
{{ Form::close() }}
</div>
<div id="form-two" class="form-area">
{{ Form::open(array('action' => 'SettingController#editPage')) }}
{{ Form::text('pagevalue', 'Default')) }}
{{ Form::submit('Save Changes') }}
{{ Form::close() }}
</div>
So basically, two seperate forms on the same page that post to two seperate methods in the same Controller - when the method is successful, it redirects them back to "settings". I won't post the methods, since tested them and they work, I believe the problem is in the routes file:
routes.php
// Checks if a session is active
Route::group(array('before' => 'require_login'), function()
{
Route::group(array('prefix' => 'settings'), function()
{
Route::get('/', 'SettingController#index');
Route::post('/', 'SettingController#editOption');
Route::post('/', 'SettingController#editPage');
});
});
Now I'm pretty sure it doesn't like the two POST routes being like that, however I cannot think of another way to do it, since the forms are on the same page. I get the error:
Unknown action [SettingController#editOption].
Since the option form comes first I guess. If I take the open form blade code out (for both), it loads the page - but obviously the form doesn't do anything.
Any help would be nice! Thanks in advance.
You can't add two same routes for different actions, because of they will be passed to first matched route and in your case to SettingController#editOption. Change your routes to :
Route::post('/option', 'SettingController#editOption');
Route::post('/page', 'SettingController#editPage');
Than in both actions you can redirect to '/': return Redirect::back(), and if error was occured:
if ($validation->fails())
{
return Redirect::to('/settings')->with_errors($validation);
}
My alternative solution for this is to create an hidden html input in each form and make the controller identify what for is submitted based in this field. So, yu can use just one route for both.