Pass variable to all views created in a Laravel Controller - php

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

Related

symfony 3 url issue

Hi I'm coming for a very simple question (I think) but i didn't found the answer or a similar case.
I'm using symfony 3 and trying to build a second menu for my administration pannel.
However, I have a problem about how I have to declare the relative url in my "href", For my main menu i used to do like this
{{ url ( 'admin' ) }}
and it worked. The fact is that now I have subfolders and many levels in my url.
The url i try to reach is myapp/admin/gameadmin, this url work when I'm going on it but when i try to put it in 'href' I have an error message which says that the route is not working.
i declared it like that ->
{{ url(admin/gameadmin) }}
I tried with different character -> admin:gameadmin, admin\gameadmin ... etc and with path instead of url i don't know if it's not the good way to declare it or if I have a problem with my controllers.
In my bundle it's organised like that :
->Controllers(folder)
->admin(folder) (You can also find my main controllers on this level)
->admingamecontroller (Where the page I try to reach is routed)
I hope i gave you all the informations, thank you for your help and sorry for my english !
The url parameter is not the the url per se (ie: admin/gameadmin), this is the route name, defined in your routing.yaml file, or in your controller annotation.
If your action is something like this:
/**
* #Route("/admin/gameadmin", name="gameadmin")
*/
public function gameAdminAction()
{
...
}
Then, to generate the route, you have to do this:
{{ url('gameadmin') }}
By doing this, all the links on your website will be up to date if you change the gameadmin url, as long as you don't change the route name.
I suggest you to read this documentation on the Symfony website: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html
Edit: As pointed by user254319, if you're not using annotations, you'll have to edit your routing.yaml config file.
gameadmin:
path: /admin/gameadmin
controller: App\Controller\Admin\AdminGameController::gameadminAction
The route name is the yaml key: gameadmin.
Related Symfony documentation: https://symfony.com/doc/current/routing.html

Laravel - how can make dynamic content with layout?

I'm wanting to create a website with laravel framework. I had made layout but now, have some zone i don't know how to set content for it. Ex: 2 zone of me are left-menu and cart (please view picture). My left-menu will get content from table: categories and cart will get content from package cart [Cart::content()].
It's on layout and of course, all page will have it. But i don't know how to give content of categories and cart() for it. Please help me
I think that you should to use View Composer.
https://laravel.com/docs/5.6/views#view-composers
Use Blade templates, as found here: https://laravel.com/docs/5.6/blade
Wherever in your page you want to print content, use the {{ $mycontent }} construct. You can also use confitionals and loop structures like #if and #foreach to loop through collections.
Then, in your controllers, you can just call the view and pass it content from your database or wherever you get it by doing something like:
return response()->view(“myView”, [“mycontent” => $content], $httpStatus);
You may opt for afterMiddleware if you want it on every page. Create a section on the master blade page (usually app.blade.php) and fill it in the middleware just like you would in any other controller. You can create a middleware by running php artisan create:middleware Cart. A file will be created at app/Http/Middleware/Cart.php.
Register the middleware in the app/Http/kernel.php file.
You may have to add a Auth::check() condition to avoid errors.

Working with CodeIgniter routes?

I think this is a route issue but I'm not sure. I have a page with this URL:
siteurl.com/kowmanger/titles/titles/edit/$id
I'm trying to find out that when I'm on this page I load the titles page it says page not found so I need to tell it that the $id is just a paramter so I can use it to get the data of the title.
UPDATE :
So I decided to change my titles controller so that there's a edit and add function inside of the titles controller that way they dont' have separate controllers when they are in fact methods.
So now I have:
kansasoutalwwrestling.com/kowmanager/titles/titles - list of titles
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
kansasoutalwwrestling.com/kowmanager/titles/titles/edit/$id - edit form
I don't have any routes set up so far for this. For some reason though I"m getting the same page for both of these page.
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
(right link url) kansasoutalwwrestling.com/kowmanager/titles/add -
addnew form
I need a route so that it'll show the correct url if the add method is accessed.
Also I need to set up a route so that if the correct edit link is accessed it sees the id attached to the end of the url and it'll accept it so that I can do a my database query to get the title data.
UPDATE: So to reiterate I have a module(subfolder) called titles. Inside of the module I have a controller called titles and inside of that controller I have 3 functions called index(), add(), edit().
I tried using Chris's suggestion on the routes but its not routing correctly. Also wanted to mention I'm using wiredesignz modular separation framework if that matters.
Any additional ideas?
Possible answer based on your post, not one hundred percent your entire structure but if i had to guess based off the post I would try this as my routes first..
$route['titles/titles/edit/(:any)'] = 'titles/titles/edit/$1';
$route['titles/titles/add'] = 'titles/titles/add';
$route['titles/titles'] = 'titles/titles';
$route['titles'] = 'titles/index';
Are you using custom routing in your configuration files ?
The general routing protocol used by codeigniter is like this:
domain.com/controller/methode/param1/param2/param3
This being said, your url
siteurl.com/kowmanger/titles/titles/edit/$id
corresponds to something like this :
class Kownmanger extends CI_Controller
{
public function titles($titles, $action, $id)
{
}
}
In case you are using sub-folders in your controllers folder, what I have just said will change, Could you please tell us what's your directory structure ?

Zend layout - how to make a menu item selected?

I have this layout file called menuAdmin.
I wish to, each time a given controller and a given action is active, to show the "li" element with a specific class.
So, I have the following on my menuAdmin.php :
<li <?php echo ($this->controller == "d" && $this->action == "a") ? "class='selectedMenuItem'" : ''; ?>>Aaaa Dddd</li>
I get nothing with this, and if I dump:
var_dump($this->controller); and var_dump($this->action); I get NULL NULL
So I believe Zend don't trigger those at that point.
Question:
How can I accomplish such a task? Should I follow this path? If so, how will my menuAdmin layout know about what controller and action is in place?
Update:
menuAdmin.php is a layout file, inside Layouts folder on Zend structure.
This is a large application and the structure in place is already like this - using layout files as menus where this is just one of them.
So $this->controller and $this->action only work inside the controller, OR if I explicitly pass it to the view. On this case, however, I would like to call it on the layout. Why there? Because by doing so, I can make one change and allow that change to be replicated all over the views that use this layout.
Regarding the above clarifications, could your answers change ?
Update 2:
I don't know if this is relevant or not but, all this menuadmin layout is called from a main layout file "layouts/main.php" and there we have: <?php echo $this->render("menuadmin.php"); ?>
Thanks again
Depending on what menuAdmin.php is you can get the controller and action in a variety of ways.
If your file is a controller you can do one of the following, they all do the same thing
$controller = $this->getRequest()->controller;
$controller = $this->getRequest()->getParam("controller");
$controller = $this->getRequest()->getControllerName();
Ideally you should use Zend_Navigation to do this though.
You should instead use Zend_Navigation as this is a built-in feature.
Edit: To answer your question about the null "controller" and "action" values; unless you have set these as view parameters from a controller or something else at the controller level (helper, plugin, etc), of course they will be empty.

Sorting List of Articles in Symfony

I have a module called articles, where I display a list of articles that I uploaded. On the index, it's just lists all of the article.
But I was wondering how I could make it so that if I click on the author, for example, it will then take me to a page that only displays article by that author (and making the url pretty ex: articles/daniel) or by a different field such as by publication. I know how to make the queries obviously, but I don't understand if I should be making new templates for these things or just change parameters on the index page and check for them.
You can use an ORM object route to achieve this with minimal coding on your part. I'm going to assume you're on Doctrine but if you're on Propel the implementation should be very similar (probably just using sfPropelRoute as your class). In your application's config/routing.yml add your custom route:
author_articles:
url: /authors/:author
param: { module: articles, action: author }
class: sfDoctrineRoute
options: { model: Article, type: list }
Note: I used the URL /authors/ instead of the /articles/ you requested so this route won't conflict with any of the actions in your articles module but feel free to use any URL you'd prefer in your config.
Clear your cache after saving those changes to make Symfony aware of the new route. What this has done is told your app to take all URLs matching /authors/* and pass them through the author action of your articles module, generating a list of objects that matches the :author parameter in the URL. The object route does this all automatically with just the configuration above.
Now in the author action of your articles module add:
public function executeAuthor() {
$this->articles = $this->getRoute()->getObjects();
}
Now you have the query result in a variable for your action template. In your authorSuccess.php template loop through the $articles array as you see fit.
To link to this route from your main list using an author's name you can use the url_for helper with the route you just created to generate the full URLs dynamically:
<?php echo $article['author'] ?>
That's all there is to it.

Categories