Laravel 5, get the HTTP status code from Blade view - php

I'm using custom error views with blade files located in resources/views/errors. My error views are made from the same template view than the normal pages so they extend my frontoffice layout. Here is the hierarchical scheme of my templates.
app.blade.php
layout.frontoffice.blade.php
someFrontOfficePage.blade.php
404.blade.php
The navbar component is included in layout.frontoffice.blade.php so I can't access it from 404.blade.php. Just to be clear, the navbar is a component as described here and is thus in yet another file.
The problem: I want to hide the links in the navbar but not the navbar itself when in an error view (such as 404.blade.php). The idea was to check if the status code is different from 200 inside my navbar component but I don't know how to access the response from a blade file.
I'd prefer to do it this way instead of copy-pasting my navbar inside a new error template to avoid redundancy.

well i had the same issue once, the solution is simple, in the error 404 blade you use extends('layouts.frontoffice') right ? then you can pass a variable to this extension, extends('layouts.frontoffice', ['code' => 404]) and in your navbar component :
#if(isset($code) && $code == 404)
//do something
#else
//do something else
#endif

Related

How Can I Make multiple template for my laravel website? (I mean frontend template not laravel template engine)

I want define more than one template for my website and I can change it in setting page.
What should I do ?
You can make more than one template like this.
Below are the two layout files in layouts folder.
app.php and admin.php
you can use these like this.
app.php
#extends('layouts.app')
#section('content')
//your current file code
#endsection
admin.php
#extends('layouts.admin')
#section('content')
//your current file code
#endsection
Hope this will help you.

add links for blade php pages

I have developed several pages for my applications. Now I need to add link for these pages. Till now I was opening these pages via url browsing.
My web.php page
// for books
Route::get('/book','BookController#create');
Route::post('/book','BookController#store');
Route::get('/book/{id}','BookController#edit');
Route::patch('/book/{id}', 'BookController#update');
// for ordered books
Route::get('/order','OrderedBookController#create');
Route::post('/order','OrderedBookController#store');
Route::get('/billSearch','OrderedBookController#searchBill');
Route::post('/billSearch','OrderedBookController#billPay');
Route::post('/billSearch/{id}', 'OrderedBookController#pay');
// for Books Out
Route::get('/booksout','BooksOutController#create');
Route::post('/booksout','BooksOutController#store');
List of pages corresponding to the routes
book.blade.php
edit.blade.php
booksin.blade.php
booksout.blade.php
local host url to browse these pages are ::
http://127.0.0.1:8000/book
http://127.0.0.1:8000/order
http://127.0.0.1:8000/billSearch // for Route::get('/billSearch','OrderedBookController#searchBill');
http://127.0.0.1:8000/booksout
How can I create link in my web apps since I am browsing my pages via routes, not by pages ?
Use route() or url() functions.
e.g. in blade view:
More in laravel docs
You may links different pages in laravel by using one of the two ways
1: By including url which you define in your route in the blade href
Route::post('/billSearch', 'OrderedBookController#pay');
therefore in your blade link
2:By using the name of the route
Route::post('/billSearch', 'OrderedBookController#pay')->name('bill);
Therefore in your blade view
just insert your routes like this:
Book --> http://127.0.0.1:8000/book
Bill Search --> http://127.0.0.1:8000/billSearch

Dynamic nav laravel 5.3

I'm building a navigation similar to wordpress basically what I want to do is add the navigation dynamically. my code in the header.blade.php looks something like:
#foreach($nav as $navs)
<li><a>$navs->title</a></li>
#endforeach
I will like the links to display in all my pages but I'm not sure how to set this in the route. Please any suggestion would be really helpful.
I'm still new to Laravel.
Sorry for not explaining properly, what I actually want is:
Once I add the navigation links and the details, it should display something like this
localhost/about-us
localhost/contact-us
So far I have got all the details and slug in the db.
e.g Controller to get all pages
class NavController extends Controller
{
public function nav()
{
$navs=Page::all();
return view('themes.first.header')->withNavs($navs);
}
}
e.g header.blade looks something like this
#foreach($navs as $nav)
<li>$nav->title</a></li>
#endforeach
The header is included on every page in master.blade so my issue is
for example I go localhost/blog
The links doesn't display but if I add route like this
Route::get('nav','NavController#nav');
Then go to /nav then it works fine. but I want something like this
localhost/about-us
and also for the links to display on every page. I hopefully I have explained properly now if not just let me know.
Please ignore suggesting code for the href="" because I've already done all that bit in my other pages etc so I know how to handle the code for the href, the part that I'm having issues is just with the navigation links not showing on all the pages.
Finally got the navigation working by using the suggestion from this thread
Proper way to make a dynamic navigation in Laravel 5
The blade syntax is wrong.
#foreach($nav as $navs)
<li><a>{{$navs->title}}</a></li>
#endforeach
Btw it's more likely that you wanted $navs as $nav and $nav->title.
If you have any problem with the routing part, share your code. Unless, I can only help with this.
In your route (example)
Route::get('page/{title}', 'PageController#details');
In PageController
public function details($title)
{
$page = Page::where('title', $title)->firstOrFail();
return view('your.page')->with(['page' => $page]);
}
In header.blade.php
#foreach($navs as $nav)
<li>{{ $nav->title }}</li>
#endforeach

Laravel 4 Won't update views content

For some reason, Laravel 4 won't update the content view that should be displayed in the browser.
For example, if I add one more item to the navigation menu, it is written on the view file, but not shown on the navigation list.
I have checked for any mispells, errors or anything related to this issue however I can't figure out if it's my mistake or it has to do with the cache.
What I've done:
Created the route for the url.
The related controller that will render the view.
Created the actual view with the content.
Added the item to the navigation file.
And I am sure that the view:
Extends with my layout
Included my content inside the #section and #stop'ed it.
And there are no errors displayed in the browser.
Also I've checked the storage folder which contains the view's cache, and I verified that the navigation file and the view file are phased correctly.
Looks like I need your advice guys.
Code as requested:
The route
Route::get('/account/profile', array(
'as' => 'account-profile',
'uses' => 'SettingsController#getProfileSettings'
));
Navigation
<li>Edit profile</li>
Controller
public function getProfileSettings() {
return View::make('account.profile');
}
View
#extends('layout.main')
#section('content')
Edit your profile...
Not yet...
#stop
First things I would do is:
Clear the view's cache folder.
check how you display the navigation, with #yield or #section, which should end with #show in the layout file.
If that didn't work maybe you could provide us with some code from the layout.

Theming and layout in yii framework

I am a newbie in Yii Framework and creating a CRM which is module based.
Using different tutorials I am able to create my own theme, but now I am stucked at one point.
In my theme, the upper <nav> and left <nav> remains the same throughout the app, until user is logged in. That's why I made it a part of my main.php, but in the login page there are no buttons to show, just simple login form with 2 textfields.
How can I implement this form in my application using custom themes?
I have tried to define a layout in that particular action but not succeeded. Any help would be appreciated.
Using a custom layout for your view is the right way to go.
You can either set the layout in the controller action or in the view.
$this->layout = "//layouts/mylayout";
Note that the default layouts column1.php and column2.php also use the main.php layout file.
Try this step by step :
Create New theme
You can create a new theme and add this to the directory
Application_Root/themes.
Look at the themes/classic directory to get an an idea of the structure of the directory.
The important file (at this stage) is :-
Application_Root/themes/views/layouts/main.php
Customise your theme contents
Copy the css, image, js files etc to the correct directory and change the main.php file to your liking. For example, if your main.php says
<link href="css/mystyle.css" rel="stylesheet">
Then you will have a file
Application_Root/css/mystyle.css
Create the content placeholder.
Somewhere in your main.php, there will be a placeholder for dynamic text, which is specified by.
<?php echo $content; ?>
Tell yii to use the theme.
Change the file Application_Root/protected/config/main.php by adding the following line just before the last line (containing the closing bracket).
'theme'=>'surveyhub'
Create the layout placeholders.
Create an HTML segment that will be written into the $contents portion of main.php. Call it for example one_column.php. The file path will therefore be Application_Root/themes/views/layouts/one_column.php In that file, where you want the dynamic text to be placed, create a placeholder.
<?php echo $content; ?>
Tell Yii to use the layout.
In the file Application_Root/protected/components/Controller.php, add or modify the layout variable to read :
public $layout='//layouts/one_column.php';
Refresh the page

Categories