Title atributes in Laravel, how to do it?
Hello, my question to Laravel masters is:
my /routes/web.php looks something like this:
Route::get('/', function () {
return view('index', []);
})->name('index');
Route::get('/services', function () {
return view('services', []);
})->name('services');
Route::get('/contact', function () {
return view('contact', []);
})->name('contact');
And then in the blade template I want to list the links, they are in the navigation bar that is located in the master layout and I yield the content of the site. Sample:
<ul class="nav-menu align-to-right">
<li>Home</li>
<li>Services</li>
<li>Contact</li>
</ul>
Surely you understand that because of SEO I don't want to mention only the name of the company/webpage, but also the subpage. For example, "MyCompany | Our services".
The problem is that these links can also be, for example, in the footer, somewhere in the text on some page, etc. I have to go everywhere and manually change the wording of the title attribute if necessary. For example, on "MyCompany | Our great service".
Well, and especially when I am in the blade template services.blade.php where I define the title of the page, it looks like this:
#section('title', '| Services')
So I just need this title to match the title attribute in a href anywhere on the site.
Perhaps I have explained and described my problem in order. The project runs on Laravel 9.x
I am new to it (but not in PHP), but I still ask you for a specific explanation, solution, which is more detailed, so that it is easier for me to understand this problem and I can learn from it for the future solutions. Thank you
Related
In laravel I have a vue.js main view template, what i want is when i click on main template this displays the child model
//my main template
<template>
<div>
<ul>
<li #click="chatWith()"></li>
</ul>
</div>
</template>
<script>
export default {
methods: {
chatWith(){
return {
template:<conversation-with></conversation-with>
}
}
},
}
</script>
but is not working, another code is working on click, but goes to template isn't
If you want it to route to a new page you need to create a route that points to that template somehow. You can either set up that template on a blade and use laravel's routing in web.php to create a route for it, then link to it like a normal <a href="/chatWith">, or you can similarly create a route using vue router instead, which would allow you to have single-page apps with better routing control.
https://laravel.com/docs/5.8/routing (make sure it's your correct laravel version)
https://router.vuejs.org/guide/
if you need it to be some kind of modal or something that's another story though
Hi I have a hyperlink from a page:
<h3>hitest</h3>
the route:
Route::get('hitest', function(){ return 'hitest message';});
There is an error:
No query results for model [App\User2].
hyper link is from a page with this url
/userpage/1
the 1 is a model object.
Shouldn't the hyperlink route to /hitest ?
Please see my other post: Strange behavior with routing and hypertext.
I'm new at web development. Is there configurations for routing? The app is hosted (not local).
As bytesarelife already mentioned, you can use the url()-function, like so:
{{ url('your/url/') }}
A better way in terms of maintainability would be to give your routes names, so you would not have to replace every url in every template once you want to change it. You can do so, by adding the name in your routes:
Route::get('hitest', function(){ return 'hitest message';})->name('getHittest');
And then you can use the route function in your view:
{{ route('getHittest') }}
I am hoping that someone can help me out with dynamic routing for urls that can have multiple segments. I've been doing some searching all over the web, but nothing I find helps me out with my specific situation.
A little background ... several years ago, I developed a CMS package for custom client websites that was built on CodeIgniter. This CMS package has several modules (Pages, Blog, Calendar, Inquiries, etc). For the Pages module, I was caching the routes to a "custom routes" config file that associated the full route for the page (including parent, grandparent, etc) with the ID of the page. I did this so that I didn't have to do a database lookup to find the page to display.
I am currently working on rebuilding this CMS package using Laravel (5.1) [while I'm learning Laravel]. I need to figure out the routing situation before I can move on with my Pages module in the new version of the package.
I know that I can do something like ...
// routes.php
Route::get('{slug}', ['uses' => 'PageController#view']);
// PageController.php
class PageController extends Controller
{
public function view($slug)
{
// do a query to get the page by the slug
// display the view
}
}
And this would work if I didn't allow nested pages, but I do. And I only enforce uniqueness of the slug based on the parent. So there could be more than one page with a slug of fargo ...
locations/fargo
staff/fargo
As with the package that I built using CodeIgniter, I would like to be able to avoid extra database lookups to find the correct page to display.
My initial thought was to create a config file that would have the dynamic routes like I did with the old version of the system. The routes will only change at specific times (when page is created, when slug is modified, when parent is changed), so "caching" them would work great. But I'm still new to Laravel, so I'm not sure what the best way to go about this would be.
I did manage to figure out that the following routes work. But is this the best way to set this up?
Route::get('about/foobar', function(){
return App::make('\App\Http\Controllers\PageController')->callAction('view', [123]);
});
Route::get('foobar', function(){
return App::make('\App\Http\Controllers\PageController')->callAction('view', [345]);
});
Basically, I would like to bind a specific route to a specific page ID when the page is created (or when the slug or parent are changed).
Am I just overcomplicating things?
Any help or direction regarding this would be greatly appreciated.
Thanks!
The way I handle this is to use two routes, one for the home page (which generally contains more complex logic like news, pick up articles, banners, etc), and a catch all for any other page.
Routes
// Home page
Route::get('/', [
'as' => 'home',
'uses' => 'PageController#index'
]);
// Catch all page controller (place at the very bottom)
Route::get('{slug}', [
'uses' => 'PageController#getPage'
])->where('slug', '([A-Za-z0-9\-\/]+)');
The important part to note in the above is the ->where() method chained on the end of the route. This allows you to declare regex pattern matching for the route parameters. In this case I am allowing alphanumeric characters, hyphens and forward slashes for the {slug} parameter.
This will match slugs like
test-page
test-page/sub-page
another-page/sub-page
PageController Methods
public function index()
{
$page = Page::where('route', '/')->where('active', 1)->first();
return view($page->template)
->with('page', $page);
}
public function getPage($slug = null)
{
$page = Page::where('route', $slug)->where('active', 1);
$page = $page->firstOrFail();
return view($page->template)->with('page', $page);
}
I keep the template file information in the database, as I allow users to create templates in the content management system.
The response from the query on the database is then passed to the view where it can be output to the metadata, page, breadcrumbs, etc.
I was also looking for the same answer that is about creating a dynamic routing in laravel i come up with this:
In routes.php
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
$str=Request::url();
$del="/public/";
$pos=strpos($str, $del);
$important1=substr($str, $pos+strlen($del), strlen($str)-1);
$important=ucfirst($important1);
$asif=explode("/", $important);
$asif1=explode("/", $important1);
//echo $important;
$post=$asif1[0];
$post1=$asif1[1];
if(isset($asif1[2]))
{
$post2=$asif1[2];
}
if(!(isset($post2)))
{
Route::match(array('GET','POST'),$important1, $asif[0].'Controller#'.$asif[1]);
}
if(isset($post2))
{ Route::match(array('GET','POST'),$post.'/'.$post1.'/{id}',$asif[0].'Controller#'.$asif[1]);
}
Route::get('/', function () {
return view('welcome');
});
Ex
if you have PostController with method hello in laravel. You can use this url http://localhost/shortproject/public/post/hello. Where shortproject is your project folder name.
Currently we are using the Laravel framework on several projects, but one issue we keep running into which i don't like is the following issue:
Lets say you have a Homepage and a Content page
HomepageController has all Homepage specific php code
ContentpageController has all Content specific php code
we have an app.blade.php that does
#yield('page')
HomepageController calls the view homepage.blade.php containing
#extends('app')
#section('page')
Some HTML part
#include('parts.top_5')
#endsection
ContentController calls the view content.blade.php containing
#extends('app')
#section('page')
Some different HTML part
#include('parts.top_5')
#endsection
Here you can see both pages include parts.top_5, the top 5 needs some specific variables to output the top5. Now the issue is we are currently copying the code for the top5 variables in both controllers or in grouped middleware but is there a better solution to generate some blade specific variables when the part is included? So a bit like running a controller function on loading the blade template?
I have been searching the internet but can't seem to find anyone with the same question. Hopefully someone can help me on this mindbreaking issue!
You can add this binding to AppServiceProvider
(or any custom ServiceProvider You want)
like this:
public function boot()
{
$view->composer('parts.top_5', function($view) {
$view->with('any_data' => 'You want');
})
}
This way any time Laravel will compose parts.top_5 view this closure method will be triggerd.
And in documentations it's here:
http://laravel.com/docs/5.0/views#view-composers
I'm organizing my website in sections, which should be visible at first glance on my navigation bar (with an active class on the appropriate link). At the moment, I'm checking for each link in the navbar if the current URL matches the one for the link, but it's only working for 1 URL in each case. It should be like this :
article ----------------> article section
article/create -----> article section
article/edit --------> article section
forum -------------------> forum section
forum/post/12345 -> forum section
Since all my "sections" use controllers, I was thinking maybe I could implement a way (maybe using the constructor) to pass a variable (section) to all the views returned by a controller, so that my layout can have access to it and set the active class on the proper link.
But I don't want to have to do return View::make('myView')->with('section', $this->section); everytime
Anyone knows how to achieve that ? Thanks.
You should use Request::segment(1) to compare it with section.
If your url is article/create and you use Request::segment(1) it will return you article and not article/create
And in fact, you don't have to pass anything to Blade in this case, because it should be visible:
#if (Request::segment(1) == 'article')
class="active"
#endif
You could also share it between all views, this way its easier to change the segment later if that changes and saves you from having to edit Request::segment(1) in all your views (if you have more)
Use:
View::share('section', Request::segment(1));
Then in every view get the value with: $section