Yield section if a certain controller is used - laravel

In my Laravel layout, I would like to display a menu bar if a certain controller is used.
How would I detect a controller inside blade?
For example:
// Layout main.blade.php
if(Controller == admin){
#yield('menu')
}
I know the syntax is wrong. Just to give you an idea what I'm trying to do.

There's a package for that: https://github.com/digithis/activehelper. It explains in detail how you can use it and it has helped me to do the same as what you're asking.
For a one-liner, you can use Route::currentRouteAction() assuming your routes are configured for example as:
Route::get('test', array('as'=>'test', 'uses'=>'TestsController#test'));
Very concrete for your question, you can use:
if (explode( '#' , Route::currentRouteAction())[0]) == 'controllerName')
{
// your code here
}

Related

Same route, different page for authenticated user or not

I want to display either welcome.blade.php or login.blade.php with the same base url / (because the login is in the main page)
Is this a good convention to check it in the web.php:
Route::get('/'), function () {
if (auth()->check()) {
// return the welcome.blade.php view
} else {
// return the login.blade.php view
}
}
Or I should do this in a different place?
Separation of concern ! It means that whatever the module/method you write should be pure from side effects and has only one job.
web.php in routes directory serves all the routes file. So if you try to put the business logic here, it will still work but it will violate the standard of framework. All business logic either should go to controllers , helpers and all the authentication related stuff should go to middlewares. Therefor, we should handle this logic inside the controllers if it is specific to single route. In our case /. Otherwise if we have to handle generic authentication stuff, we should go into middleware.
Therefor, the flow would look something like this
User hit the route /
Through the web.php, it redirected to particular controller let say HomeController.
Inside the HomeController , you will put the same condition and render the view accordingly. i.e
if (auth()->check()) {
// return the welcome.blade.php view
} else {
// return the login.blade.php view
}
We can achieve the same thing in web.php but it will not be according to the framework standard. Further, If you have to do more stuff other than just rendering view, it will get more complicated in web.php.
I hope it will answer your question.
Thanks,

Laravel Blade Template - Put php built-function or maybe laravel helper inside yield

do you ever use PHP built-in function inside blade yield ?
For example can we do something like this :
// master layouts
#yield(ucwords('title'))
// view
#section('title', $title)
Note: $title is from controller
I've already try the first example, but it doesn't work. It doesn't output the $title on my view. Right now I am using this in all of my views
// master layouts
#yield('title')
// view 1
#section('title', ucwords($title))
// view 2
#section('title', ucwords($title))
// view 3
#section('title', ucwords($title))
But I think on second example, I'm not DRY my code because I always repeating the ucwords() on each my view. Can we using it on master layout right on yield declaration?
Thank you guys, have a nice work!
You can make your own blade directive for example if you want to make a #ucfirst() then do something like this in AppServiceProvider .
Blade::directive('ucfirst', function ($expression) {
return ucfirst($expression);
});
Past this into boot()
or on each section() you can extend the main layout #section('title', ucwords($title)) or make the helpers like i mentioned above
as you mentioned above you can use yield()
#yield('title',ucwords(strtolower('Your title')))

Laravel Nova, change search results HTML

I have a simple Nova integration like this :
I want somehow to make the Name field to be an anchor. So, to make it a link the same way that view button does. Any ideas? (Nothing on docs)
Text::make('name')
->asHtml()
->displayUsing(function ($name) {
return {{$name}}
})
Text::make('Name', function () {
return ''.$this->name.'';
})->asHtml()

Remove controller's name from url. Codeigniter

can someone help me with this. I want to remove controller name from the url like this
www.site-name.com/controller_name/controller_functions/controller_arguments
to this
www.site-name.com/controller_arguments
for instance:
www.site-name.com/blog/display/blog-title
to this:
www.site-name.com/blog-title
Below is my controller class
class Blog extends CI_Controller {
public function index() {
$data['blogs_data'] = $this->Blog_model->get_blogs();
$data['main_view'] = "blog/blog_layout";
$this->load->view('layouts/main', $data);
}
public function blog_display($page_url) {
$data['blog_data'] = $this->Blog_model->get_blog($page_url);
$data['main_view'] = "blog/blog_detail_layout";
$this->load->view('layouts/main', $data);
}
}
as your can see from my controller the URL of my site to display blog going to be like this www.website-name.com/blog/blog_display/($page_url) <== whatever the $page_url going to be. Now, what I want is to be like this www.website-name.com/($page_url) <= so straight to the $page_url.
Thanks
Go to application->config->routes.php
Then you can set a route to "blog/display/blog-title" as "blog-title"
Add the below line to your routes.php file
$route['blog-title'] = 'blog/display/blog-title';
You can replace "blog/display/blog-title" to "blog-title" then.
define route in routes.php file.
route['controller_arguments']='controller_name/controller_functions';
on which onclick you are showing this url.define there this.
<a href="<?php echo base_url(); ?>controller_arguments">
</a>
I imagine that your slugs are generated so you can't just write all your slugs into your routes.
In you specific case you need something like this in your routes:
$route['(:any)] = 'blog/display/$1';
Do bare in mind that your routes are used from top to bottom. So if you have this route as your first one the rest of your site might not work.
So in case of a entire blog stucture you might want something like:
$route[''] = 'blog/index'; // For first page without pagination
$route['(:num)] = 'blog/index/$1'; // Blog article pagination (for second page and all other pages)
$route['(:any)] = 'blog/display/$1'; // Blog article detail
To avoid some problems in the future with having a route that is just a (:any) param, you might want to add an extra segment in that blog detail article.
Like so:
$route['detail/(:any)] = 'blog/display/$1'; // Blog article detail
For more information about this subject take a quick look at the docs here: Codeigniter routing system

Laravel 4: if statement in blade layout works strange

Could someone explain me why I get blank screen with printed string "#extends('layouts.default')" if I request page normally (not ajax)?
#if(!Request::ajax())
#extends('layouts.default')
#section('content')
#endif
Test
#if(!Request::ajax())
#stop
#endif
I'm trying to solve problem with Ajax, I don't want to create 2 templates for each request type and also I do want to use blade templates, so using controller layouts doesn't work for me. How can I do it in blade template? I was looking at this Laravel: how to render only one section of a template?
By the way. If I request it with ajax it works like it should.
Yes #extends has to be on line 1.
And I found solution for PJAX. At the beginning I was not sure this could solve my problem but it did. Don't know why I was afraid to lose blade functionality if you actually can't lose it this way. If someone is using PJAX and needs to use one template with and without layout this could be your solution:
protected $layout = 'layouts.default';
public function index()
{
if(Request::header('X-PJAX'))
{
return $view = View::make('home.index')
->with('title', 'index');
}
else
{
$this->layout->title = 'index';
$this->layout->content = View::make('home.index');
}
}
Try moving #extends to line 1 and you will see the blade template will render properly.
As for solving the ajax problem, I think it's better if you move the logic back to your controller.
Example:
…
if ( Request::ajax() )
{
return Response::eloquent($books);
} else {
return View::make('book.index')->with('books', $books);
}
…
Take a look at this thread for more info: http://forums.laravel.io/viewtopic.php?id=2508
You can still run your condition short handed in the fist line like so
#extends((Request::ajax())?"layout1":"layout2")

Resources