zend framework 2 changing layout in the controller - layout

i want to change my layout in the controller. (i know how to change per module, but do need to change certain pages on my application, hence my need to use the controller).
i have done the following but it still render the default layout.
'template_map' => array(
'layout/homepage' => __DIR__ . '/../view/layout/homePageLayout.phtml'
)
controller
$viewModel = new ViewModel();
$viewModel->setTemplate('layout/homepage');
return $viewModel;
i noticed from this answer that i am supposed to use this instead
$this->layout('layout/different');
but the author does not clarify how that is supposed to be used. i.e instead of setTemplate is their a setLayout.
Thank you in advance for kind help.

i got the answer from this page.
all we need to do is this(same as above, accept that in the action we do the following)
public function someAction()
{
$this->layout("layout/homepage");
return new ViewModel(array(
));
}

Related

Why is my dynamic Gatsby page not working

I'm trying to create dynamic pages based on a database that grows by the minute. Therefor it isn't an option to use createPage and build several times a day.
I'm using onCreatePage here to create pages which works fine for my first route, but when I try to make an English route somehow it doesn't work.
gatby-node.js:
exports.onCreatePage = async ({ page, actions: { createPage } }) => {
if (page.path.match(/^\/listing/)) {
page.matchPath = '/listing/:id'
createPage(page)
}
if (page.path.match(/^\/en\/listing/)) {
page.matchPath = '/en/listing/:id'
createPage(page)
}
}
What I'm trying to achieve here is getting 2 dynamic routes like:
localhost:8000/listing/123 (this one works)
localhost:8000/en/listing/123 (this one doesn't work)
My pages folder looks like this:
pages
---listing.tsx
---en/
------listing.tsx
Can anyone see what I'm doing wrong here?
--
P.S. I want to use SSR (available since Gatsby v4) by using the getServerData() in the templates for these pages. Will that work together with pages created dynamically with onCreatePage or is there a better approach?
According to what we've discussed in the comment section: the fact that the /en/ path is never created, hence is not entering the following condition:
if (page.path.match(/^\/en\/listing/)) {
page.matchPath = '/en/listing/:id'
createPage(page)
}
Points me to think that the issue is on your createPages API rather than onCreatePage, which means that your english page is not even created.
Keep in mind that onCreatePage API is a callback called when a page is created, so it's triggered after createPages.
If you add a console.log(page.path) you shouldn't see the English page in the IDE/text editor console so try debugging how are you creating the /en/ route because it seems that onCreatePage doesn't have any problem.

How to redirect to ASP.NET Ideneity login page?

Using ASP.NET Identity, if I want to construct an <a> element that links to the Login page I can use Razor Helpers and some magic strings:
<a asp-area="Identity" asp-page="/Account/Login">Login</a>
(Not the question at hand, but I'd be happy if someone would tell me why this convoluted collection of magic strings is somehow better than hardcoding the "/Identity/Account/Login" relative URL.)
My question is: If I want to return a redirect from a Handler routine (i.e., the C# code behind a razor page), is there some recommended magic that I should be using to get a relative URL to the Login page?
In other words, is there something better than:
return Redirect("/Identity/Account/Login");
Use it this way:
return RedirectToPage("/Account/Login", new { area = "Identity" });
You may be missing RazorPages. Depending on how your mapping occurs in your program.cs, either add in your Web.config
services.AddRazorPages;
or in your .Net6 Programs.cs use
builder.Services.AddRazorPages;
or if you are using endpoints, replace
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);
});
with
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});

Loading profile only with parameter without the need to key in Kohana 3.x

I'm new to Kohana 3.x. Would you like a website with Kohana with User profile style twitter. Example: https://twitter.com/maronems to load the profile is passed only paramentro maronems without the need to pass the key = parameter. Please can someone help me?
By "key = parameter" I assume you mean something like http://twitter.com?user=maronems right? This is ugly, we can do better.
Let's look at making your URLs look like http://twitter.com/maronems instead.
You'll want to look at Kohana's routing system.
Take a look at this route:
Route::set('username route', '<username>')
->defaults(array(
'controller' => 'Profile',
'action' => 'index',
));
Firstly, it's called username route, this is an aribitrary name,
but a good one because it's intent is clear.
Next look at the regex pattern <username>. This route is going to capture the username and store it in a variable called username.
Now notice that the route doesn't have to specify the controller and action. The routing system will get those from the default values. In this example you'll need a controller called Controller_Profile with an action called action_index.
So let's look at the controller now:
<?php
class Controller_Profile extends Controller {
function action_index()
{
echo 'Hello ' . $this->request->param('username');
}
}
Of course you shouldn't user echo like this in classes, but to illustrate the point, if you visit example.com/maronems you should see Hello maronems echoed out.

How to make sure a user can only see and access their own data in Yii

In Yii, is there a best way to make sure a user can only see and access their own data in Yii?
I thought an Admin should be able to see anything, but for now, I'll cross that bridge later.
Thanks
Look into scopes. Default scopes will be your friend:
http://www.yiiframework.com/doc/guide/1.1/en/database.ar#named-scopes
Because the defaultScopes array is inside of a function, you can also do conditional default scopes:
public function defaultScope()
{
$t=$this->getTableAlias(false,false);
if(Yii::app()->user->notAdmin()) {
return array(
'condition'=>"$t.<column_name> = :<columnName>",
'params'=>array(':<columnName>'=>Yii::app()->user->notAdmin),
);
}
else return array();
}
Edit: Note that this can get you in trouble down the road if you aren't careful. See this issue on the Yii site for more info.
There is no way Yii will do this for you, you'll do it on your own, but it's fairly straight forward.
You can consider scopes, or look into Relations and base them all on current user. For example, to get all posts by a user, you can do:
$posts = Post::model()->findAll(); //WRONG
$posts = Yii::app()->user->posts(); //RIGHT (Should define the relation in the User model)
Check out a solution which I wrote:
http://www.yiiframework.com/forum/index.php/topic/42735-restrict-users-to-only-editingdeleting-their-own-entries/page_gopid_237608#entry237608

Expressionengine hooks

I have a safecracker form that submits an entry. The form consists of title, url_title, and description. I want to create an extension hook that filters out certain words if they exist in the title of the entry.
I already have a function that take care of the cleaning function clean(){....}. I understand that we need to use an extension hook so we can clean the title upon saving the entry.
What extension hook do i need to use for that. can you give me a complete example of an extension hook. I'm very good with PHP but still new to hooks and how they should be implemented. I already read the EE documentation but still find some confusion of how a hook is used
First head over to http://pkg.io/ and get your base extension file.
You'll probably want to use the 'safecracker_submit_entry_start' hook to throw an error if unclean word is entered. The most important part of the extension is registering the method and hook you want to use, otherwise none of the code will run.
Your code should look something like this:
public function activate_extension()
{
// Setup custom settings in this array.
$this->settings = array();
$data = array(
'class' => __CLASS__,
'method' => 'clean', // point to the method that should run
'hook' => 'safecracker_submit_entry_end', // point to the hook you want to use to trigger the above method.
'settings' => serialize($this->settings),
'version' => $this->version,
'enabled' => 'y'
);
$this->EE->db->insert('extensions', $data);
}
Once the method has been called you can start your cleaning. Make sure you pass the safecracker object to your clean method when defining it. For example:
public function clean($sc){
print_r($sc);
}

Resources