Block directory in magento [closed] - magento

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.

Related

Laravel: advantages or disadvantages of using slug instead of id [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 3 years ago.
Improve this question
I was wondering of what could be the advantages or disadvantages of using slug instead of id when looking for a model or for urls.
Because there is a possibility to bind directly the url so that in the web.php file, this url /users/{user} will become the following:
with id:
domain.com/users/1
or with slug:
domain.com/users/IQii33XAQhldEK
Thanks
IQii33XAQhldEK is not a slug, a slug would be domain.com/users/patrissol-kenfack.
In the case of the slug, the 2 benefits are:
Better readability for humans,
Better for SEO, because Google will see the keywords in the URL.
If you use id in the url, it could mean that users would be able to know what the id of the resource they are viewing are. Usually, this won't be much of an issue, but if proper security practices are not followed, users could modify resources using this id by passing it implicitly (So make sure to use $fillable or $guarded) . For example, a malicious user could simply put a
<input type="text" name="id" value="<the-id-of-any-other-resource>"
And if you insert or update models with $request->all(), it could end up modifying the id of that resource.

Controller under a controller: Laravel 5.5 [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 4 years ago.
Improve this question
I have two controllers:
AdminController
ProductController
AdminController has some functions:
index
create
etc
ProductController also has some functions:
index
create
etc
In admin dashboard there is a button Product. When I will click it it will call ProductController index page.
Then in Product button subsection create, edit, delete etc will be available.
at last my url will be: admin/product/create
How is this possible? Give me idea.
Have a look at Routing Prefixes. This way, the admin slug is always there but you can bind the rest of the actions from there. Furthermore, you can have multiple prefixes within each other if you'd like. If you are not familiar with the routes.php, I'd suggest reading through the documentation and just simply play with it a bit.

Why use query strings? [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 5 years ago.
Improve this question
I am making a website using laravel. I have a search page where users can search for products and a SearchController which does the actual searching.
Right now, I am passing the form data to the controller with a post method (search string, filters, etc.), it works brilliantly!
However, I see that almost every website passes the search parameters in the URI using a query string. Why is tht better? Should I be doing the same?
Thanks!
It's good to keep query parameters in uri for SEO purpose. If you use post method, when you refresh page you won't get data. If you use get method, even page is refreshed you will get data. If you pass query in uri, you can directly get the data by accessing that url.
The search parameter in URL is helpful when the user wants to search more item related to that without refreshing the page. You can do it by ajax and php.

How to load the layout at runtime in Magento?

I know that we can design the layout in *.xml then in the action just invoke loadLayout, and renderLayout to render the blocks/views.
But, I have a question is:
- How can I load the layout at runtime?
If we have an action which does not really design its layout and will be decided how to render at runtime.
You can please consider the answer from the question for more clear.
Writing a new answer because it seems that you actually DO still want to render, you just want to render a different route's layout XML updates. I believe the _forward() method from Mage_Core_Controller_Varien_Action will allow you to do what you are describing with the least amount of pain.
You should add your action controller directory ahead of the catalog directory, create a ProductController with a viewAction, and check customer is not logged in - in this check you would call $this->_forward('customer','account','login');.
This approach though is going to require more effort in order to be usable, as I imagine that you want the user to be sent to the product page upon login. Have you seen Vinai Kopp's Login Only Catalog module? It should do this for you.
loadLayout() and renderLayout() just execute block output method toHtml() (usually) and take the resulting strings and apply them to the response object via appendBody(). In an action controller you can just call $this->getResponse()->setBody('response string'). How you build the string is up to you.
You can also use Mage_Core_Block_Flush to immediately send output to the browser without using the response object.

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