Get Controller and Action name in your view template? [duplicate] - laravel

This question already has answers here:
Get Laravel 5 controller name in view
(8 answers)
Closed 3 years ago.
I wonder if how i can get the Controller name and Action name in the Blade template/View?
Thanks in advance!

A few options.
Pass variables to the view from your controller.
Set a global view variable
use view composer.
You need to choose one of these depending on your use case.
Route::getCurrentRoute()->getAction();
Route::currentRouteAction();
Route::currentRouteName();

Related

Laravel optional parameter with slash in route [duplicate]

This question already has answers here:
Optional parameter in the middle of a route
(3 answers)
Closed 3 years ago.
I'm having trouble creating ONE laravel route that has an optional parameter. The following achieves the behaviour I want:
Route::get('/{locale}/donate', 'MyController#index')->name('donation.index');
Route::get('/donate', 'MyController#index')->name('donation.index');
Both the urls /fr/donate and /donate will load the MyController index(). However, when I do this:
Route::get('/{locale?}/donate', 'MyController#index')->name('donation.index');
The /donate will not load the MyController index(). How do I make the locale argument an optional segment in the url?
optional parameters can be located only at the end of the url
Route::get('/donate/{locale?}', 'MyController#index')->name('donation.index');
In my opinion there is no way to define the parameter as optional. Here are other ways to solve your problem.
First
Create a subdomain for that
Second move your optional parameter to the last. Now your route will look like this
Route::get('/donate/{locale?}', 'MyController#index')->name('donation.index');

Symfony the same variable for all Twig templates

I am writing my own CMS using Symfony 3 right now and i have problem with include the same variables in all controllers.
For example:
I want render logo which i have just upload in my admin panel so i keep URL to it in database and fetch with other data like meta (site descryption, site title etc.) in controller - passing it as array and call in base.html.twig
However base.html.twig extends all other twig templates which i use in controllers so i must fetch it in all of them.
Is there some nice solution for my problem ?
Posting an answer based on your comment.
Use addGlobal() method in your base controller like so:
$this->get('twig')->addGlobal('variable', $variable);
Where $variable is the global variable you need.

how can i add #somelink to laravel route

I want to create a laravel route that links to a named anchor in a page. Could someone help with how to achieve that. Example: return Redirect::route('admin.articles.edit#somelink', $article->id);
In this case the route is in a controller which is redirecting back to the pages' comments section
I was looking for this and stumbled upon this older question.
The "possible duplicate" has an answer for the case where the link is created in a view (i.e. in html). It may not be immediately clear that this can also be used in returning a redirect route from a controller, as OP seemed to require (some years ago :).
At least in Laravel 5.5, it is possible to use this in the redirect()->to() function, e.g.:
return redirect()->to(
route('admin.articles.edit', [$article->id]) . "#somelink"
);
Based on this answer.

Block directory in magento [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am new to Magento. I just want to know in which situation we write the code in Block directory? Why we cannt write same code in Controllers?
I read articles for this but does not get the answer.
Thanks in advance.
Take a look # Magento for Developers: Part 4 - Magento Layouts, Blocks and Templates
Unlike many popular MVC systems, Magento's Action Controller does not
pass a data object to the view or set properties on the view object
(with a few exceptions). Instead, the View component directly
references system models to get the information it needs for display.
One consequence of this design decision is that the View has been
separated into Blocks and Templates. Blocks are PHP objects, Templates
are "raw" PHP files (with a .phtml extension) that contain a mix of
HTML and PHP (where PHP is used as a templating language). Each Block
is tied to a single Template file. Inside a phtml file, PHP's $this
keyword will contain a reference to the Template's Block object
We write the code in Block directory for creating blocks.
Example: we need to show a block at the left sidebar. controller doesn't need for this.

Separate controller for home/index site in cakephp? Best Practice? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I´m starting to developing with cake php. I have designed a DB model and baked my mvc´s.
Now i want a index / home site. This site should be an overview of possible actions which a user can do.
Should I use an app_controller for that or route to an existing controller even if that controller has nothing to do with a home site, or should I use a separate controller with no model just for displaying the overview and edit the route of / to point at this new Home Controller?
Whats the best practice for that?
Your question is a little vague to me. I am going to assume that by "site" you mean "page".
If by "overview of possible actions which a user can do" you mean a static page with links, then use the provided PagesController, and create a view at app/views/pages/home.ctp.
If by "overview of possible actions which a user can do" you mean a dynamic page with links and data, then create a controller action to feed the page the correct data.
Where that controller action goes should depend on where the data comes from.
If it lists the latest posts, create a PostsController::home() action.
If it needs data from the User model in order to determine what to display, then create a UsersController::home() action.
Finally, if you are mixing data from many models with no clear winner, or you are actually creating a home "site" instead of a "page", create a HomeController or DashboardController.
Read this post by teknoid for a nice succinct way of loading in arbitrary models when needed.

Resources